2013-10-15 50 views
3

我有一个看起来像这样的核心数据模型。将更多对象提取到实体后,核心数据关系丢失

enter image description here

里面一个的tableview我打开了我所有的约会。在我custom cell内的UILabel中,我按如下方式设置约会位置名称。

NSString *info = appointment.location.label_vrij; 

起初一切正常OKE,但是当我加载多个预约到我database。 所有信息字符串变为NULL。经过一些调试后,我注意到appointment.location返回NULL

这是我NSFetchRequest看起来像

RKManagedObjectStore *store = [[SanMaxDataModel sharedDataModel] objectStore]; 
    NSManagedObjectContext *context = store.mainQueueManagedObjectContext; 

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Appointment"]; 
    NSString *relationshipKeyPath = @"location"; // Set this to the name of the relationship on "A" that points to the "B" objects; 
    NSArray *keyPaths = [NSArray arrayWithObject:relationshipKeyPath]; 
    [fetchRequest setRelationshipKeyPathsForPrefetching:keyPaths]; 
    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"dateStart" ascending:YES]; 
    fetchRequest.sortDescriptors = @[descriptor]; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat: 
           @"tijd_dag = %@",strDate]; 
    [fetchRequest setPredicate:predicate]; 
    NSArray *matches = [context executeFetchRequest:fetchRequest error:nil]; 
    appointments = [matches mutableCopy]; 

所以我觉得我的关系只是搞砸?

编辑

我使用Restkit映射我的约会到我的核心数据库。从下面的评论中,我决定关闭选项标志关闭约会实体内的位置属性。因为约会应该总是有一个位置。

现在当我加载第一天。一切正常oké。但是,当我尝试加载第二天起来,我得到的错误:The operation couldn\U2019t be completed. (Cocoa error 1570.)现在,当我一起来看看详细的错误

DetailedError: { 
    NSLocalizedDescription = "The operation couldn\U2019t be completed. (Cocoa error 1570.)"; 
    NSValidationErrorKey = location; 
    NSValidationErrorObject = "<Appointment: 0x864e2f0> (entity: Appointment; id: 0x9272c70 <x-coredata://9692683D-3077-4362-9253-652AC5B36444/Appointment/p9> ; data: {\n autouur = 1;\n breekuur = 0;\n data1 = \"\";\n data2 = \"\";\n data3 = \"\";\n data4 = \"\";\n data5 = \"\";\n data6 = \"\";\n data7 = \"\";\n data8 = \"\";\n data9 = \"\";\n dateStart = \"2013-10-23 09:00:00 +0000\";\n dateStop = \"2013-10-23 09:30:00 +0000\";\n duration = 30;\n email = \"\";\n entryID = 774294984959;\n info = \"\";\n \"is_blocked\" = 0;\n \"is_except\" = 0;\n \"is_free\" = 1;\n \"is_moved\" = 0;\n \"is_vert\" = 0;\n locatieID = 773150;\n location = nil;\n multiID = nil;\n serverEntryID = 774294984959;\n serverLocatieID = 773150;\n sms = \"\";\n \"tijd_dag\" = 20131023;\n \"tijd_uur\" = 900;\n})"; 
} 

这是我的核心数据加载了JSON

-(void)getAppointmentsForDate:(NSString *)date forUserID:(NSString *)userID{ 
    API *api = [API new]; 
    RKManagedObjectStore *store = [[SanMaxDataModel sharedDataModel] objectStore]; 
    NSLog(@"store is %@",store); 
    NSManagedObjectContext *context = store.mainQueueManagedObjectContext; 
    RKObjectManager *objectManager = [api mapAppointments]; 

    NSString *urlString = [NSString stringWithFormat:@"/doctor/1.0/json/nl/appointments/get-by-date/apikey/%@?uid=%@&date=%@",APIKey,userID,date]; 
    // NSString *urlString = [NSString stringWithFormat:@"/doctor/1.0/json/nl/appointments/get-by-date/apikey/%@?uid=77382&date=%@",APIKey,date]; 


    NSURLRequest *request = [objectManager requestWithObject:nil method:RKRequestMethodGET path:urlString parameters:nil]; 
    RKManagedObjectRequestOperation *operation = [objectManager managedObjectRequestOperationWithRequest:request managedObjectContext:context success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 
     NSLog(@"TILL HERE IN METHOD"); 
     NSError *error = nil; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"appointmentsLoaded" object:self]; 
     [[SanMaxDataModel sharedDataModel] saveToPersistentStoreAsync:&error]; 
    } failure:^(RKObjectRequestOperation *operation, NSError *error) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                 message:[error localizedDescription] 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
     NSLog(@"Hit error: %@", error); 

    }]; 
    [objectManager enqueueObjectRequestOperation:operation]; 
} 
+0

如何加载更多约会?你可以发布该代码吗?并且,在插入新的托管对象或对核心数据进行任何更改后,是否确保“保存:”托管对象上下文? –

+0

我在任何地方都可以保存,当我改变某些东西时。 – Steaphann

+0

如果在加载更多约会后出现问题,则问题可能出在您加载约会时。其他事情可能会在那里发生。 –

回答

2

它看起来像你的问题是默认情况下RestKit替换执行新映射时的关系内容。你需要告诉它你想要与旧的关系数据。通过设置这样做:

relationshipMapping.assignmentPolicy = RKAssignmentPolicyUnion; 

如果您使用RestKit的旧版本,你需要使用RKUnionAssignmentPolicy

其中relationshipMapping是您的RKRelationshipMapping实例。

+0

你救了我的一天!有史以来最好的赏金! – Steaphann