다음과 같은 기본적인 문제가 있습니다 :CoreData - 기존 엔티티에 새로운 관계 추가하기
두 개의 엔티티 인과 부서가 있습니다.
새로운 사람을 추가하기 전에 해당 부서가 이미 존재하지 않는지 확인하고, 그렇다면 새로운 사람을 기존 부서에 연결하십시오. 새로운 부서 관계
간단한 삽입 :
Department *department = (Department *)[NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.context];
department.groupName = @"Office of Personnel Management";
Person *person1 = (Person *)[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.context];
person1.name = @"John Smith";
person1.birthday = [self dateFromString:@"12-1-1901"];
person1.department = department;
Person *person2 = (Person *)[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.context];
person2.name = @"Jane Doe";
person2.birthday = [self dateFromString:@"4-13-1922"];
person2.department = department;
department.manager = person1;
department.members = [NSSet setWithObjects:person1, person2, nil];
마지막 줄은 연결한다 - 그 괜찮습니다.
그러나 나는 위의 코드의 실행 후, 다음을 수행하려는 경우 :[self checkForExistingDepartment:@"Office of Personnel Management"];
if(self.existingDepartment) {
// here is my Problem number 1:
// department = ???
(NSEntityDescription *) department = self.existingDepartment;
} else {
Department *department = (Department *)[NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.context];
department.groupName = @"Office of Personnel Management";
}
Person *person1 = (Person *)[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.context];
person1.name = @"John Smith the second one";
person1.birthday = [self dateFromString:@"12-1-1901"];
person1.department = department;
// former line for adding new: department.members = [NSSet setWithObjects:person1, nil];
// and here is my problem number 2:
// I think I need something like: [NSSet addObjects:person1, nil];
을 짧은 형식으로 내 문제는 테이블 부서에서 항목을 복제됩니다.
누군가는 고급 SQL 지식을 갖춘 초보자에게 좋은 CoreData 튜토리얼을 알고있을 것입니다. (Google에서 검색하거나 개발자 설명서를 읽는 것이 내가 생각했던 것만 큼 도움이되지 않는다.)
나를 초보자로 생각하면 내가 올바른 방향인지 여부는 중요하지 않으므로 누구나 확인할 수 있습니까?
감사 인사,
마티아스