2013-09-05 7 views
1

데이터를 코어 데이터에 저장하려고합니다. (주 모델 및 관련 모델). 죽이기 후에 저장하면 주 모델은 저장되지만 관련 모델은 저장되지 않습니다. 나는 다음과 같은 코드를 가지고 : 나는 인수가 저장되지 않는 응용 프로그램을 죽일 때관련 데이터 저장 코어 데이터

NSManagedObjectContext *context = [self managedObjectContext]; 


    Trace *trace = [NSEntityDescription 
        insertNewObjectForEntityForName:@"Trace" 
        inManagedObjectContext:context]; 


    trace.totalDistance = self.totalDistance; 
    trace.averageSpeed = self.averageSpeed; 
    trace.theBestSpeed = self.theBestSpeed; 
    trace.stopTime = [NSNumber numberWithInt: self.stopTime]; 
    trace.time = self.time; 
    NSMutableArray *nsa = [[NSMutableArray alloc] init]; 
    for (int i=0;i<locations.count;i++){ 
    CLLocation* cl =(CLLocation*) locations[i]; 
     Argument *point = [NSEntityDescription 
          insertNewObjectForEntityForName:@"Argument" 
          inManagedObjectContext:context]; 

    point.latitude = [NSNumber numberWithDouble: cl.coordinate.latitude]; 
    point.langitude = [NSNumber numberWithDouble: cl.coordinate.longitude]; 
    point.speed = self.speeds[i]; 
    [nsa addObject:point]; 
    } 
    NSOrderedSet * ns = [[NSOrderedSet alloc] initWithArray:nsa]; 
    trace.points = ns; 


    NSError *error; 
    if (![context save:&error]) { 
     NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]); 
    } 

하지만 추적이 저장됩니다. Shat가 문제를 일으킬 수 있습니까 ??

+0

은 확실 locations.count'가 큰 '것을 있습니까 0보다? –

답변

0

간단히 말해서, 저장하기 전에 앱을 종료하면 저장되지 않습니다. 놀라움이 없습니다.

0

먼저 엔티티 관계를 올바른 방법으로 사용하고 있는지 확인하십시오.

xcode 모델 편집기에서 엔터티를 선택하고 관계로 이동하여 Argument 개체와 'To-Many'관계가 있는지 확인하십시오. 개체를 연결하는 속성을 사용하지 마십시오. 작동하지 않습니다.

그런 다음 (아래 이미지를 확인할 수 있음) 새 오브젝트를 '추적'오브젝트와 연결하고 [traceObject addArgumentsObject:argument]을 사용하여 동일한 방법으로 제거하십시오. 관계의 model editor

재산권 - - 더 Core Data Programming guide

모델 확인을 위해

property of the relationship

선미를