2011-02-15 1 views
1

다음과 같은 기본적인 문제가 있습니다 :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에서 검색하거나 개발자 설명서를 읽는 것이 내가 생각했던 것만 큼 도움이되지 않는다.)

나를 초보자로 생각하면 내가 올바른 방향인지 여부는 중요하지 않으므로 누구나 확인할 수 있습니까?

감사 인사,

마티아스

답변

0

당신은 if/else 문 내부의 부서 변수를 정의하고 다음 다른 하나 개의 외부를 사용하고 있습니다. 같이 당신의 if을 변경해보십시오 :

[self checkForExistingDepartment:@"Office of Personnel Management"]; 
if(self.existingDepartment) { 
    department = self.existingDepartment; 
} else { 
    department = (Department *)[NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.context]; 
    department.groupName = @"Office of Personnel Management"; 
} 

비록를 우리가 아주 많이 당신을 도울 수 checkForExistingDepartment의 코드를 보지 않고. . .

0

죄송 체크 기능은 다음과 같습니다 :

- (void) checkForExistingDepartment:(NSString *)searchDepartment { 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Department" initManagedObjectContext:self.context]; 
    [fetchRequest setEntity:entity]; 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"country" ascending:YES selector:nil]; 
    NSArray *descriptors = [NSArray arrayWithObject:sortDescriptor]; 
    [fetchRequest setSortDescriptor:descriptors]; 

    NSString *query = searchDepartment; 
    if(query && query.length) { 
     fetchRequest.predicate = [NSPredicate predicatWithFormat:@"country contains[cd] %@", query]; 
    } 

    NSError *error; 
    self.fetchedResultsController = [[NSFetchedResultsController alloc] 
            initWithFetchRequest:fetchRequest 
            managedObjectContext:self.context 
             sectionNameKeyPath:@"section" 
               cacheName:@"Root"]; 
    self.fetchedResultsController.delegate = self; 
    [self.fetchedResultsController release]; 
    if(![[self fetchedResultsController] performFetch:&error]) { 
     NSLog(@"Error %@", [error localizedDescription]); 
    } 

    self.existingDepartment = [self.fetchedResultsController objectAtIndexPath:0]; 
    [fetchRequest release]; 
    [sortDescriptor release]; 
}