在将更多对象提取到实体后,核心数据关系丢失 [英] Core data relationship lost after fetching more objects into the entities

查看:90
本文介绍了在将更多对象提取到实体后,核心数据关系丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似这样的核心数据模型。

I have a core data model that looks like this.

在表格内载入我的所有约会。在自定义单元格里面的 UILabel 我设置约会位置名称如下。

Inside a tableview I load up all my appointments. On a UILabel inside my custom cell I set the appointments location name as follows.

NSString *info = appointment.location.label_vrij;

起初,所有的东西都可以工作,但是当我加载更多约会到我的数据库
所有信息字符串变为 NULL 。经过一些调试,我注意到 appointment.location 返回 NULL

At first everything works Oké, but when I load more appointments into my database. All the info strings goes NULL. After some debugging I noticed that also appointment.location returns NULL.

这是我的NSFetchRequest看起来像

This is how my NSFetchRequest looks like

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];

所以我认为我的关系只是乱七八糟?

So I think my relationship is just messed up ?

EDIT

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

I'm using Restkit for mapping my appointments into my core database. From the comments below I decided to turn off the option flag off the location attribute inside the Appointment entity. Because an appointment should always have a location.

现在,当我加载第一天。一切工程oké。但是当我尝试加载第二天,我得到错误:操作couldn\U2019t完成。 (Cocoa错误1570)现在,当我看一看详细的错误

Now when I load up the first day. Everything works oké. But when i try to load up the next day , I get the error: The operation couldn\U2019t be completed. (Cocoa error 1570.) Now when I take a look at the detailed Error

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的方式

This is how I load up the JSON in core data

-(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];
}


推荐答案

默认情况下RestKit会替换执行新映射时关系的内容。你需要告诉它,你想要与旧的新的关系数据。通过设置:

It looks like your problem is that by default RestKit replaces the contents of relationships when a new mapping is performed. You need to tell it that you want the new relationship data with the old. Do this by setting:

relationshipMapping.assignmentPolicy = RKAssignmentPolicyUnion;

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

If you're using an older version of RestKit you will need to use RKUnionAssignmentPolicy.

其中 relationshipMapping RKRelationshipMapping 实例。

Where relationshipMapping is your RKRelationshipMapping instance.

这篇关于在将更多对象提取到实体后,核心数据关系丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆