简体   繁体   English

CoreData-不在NSSet上保存关系或外键详细信息

[英]CoreData - Not saving relationships or foreign key details on NSSet

I googled and looked at most of the Core data questions before deciding to post in SO. 在决定发布SO之前,我先搜索并查看了大多数核心数据问题。 It's kind of weird and I don't know where to go from here. 有点奇怪,我不知道从这里去哪里。

I'm building an iOS app and I'm using RestKit, Core Data and Objective Record. 我正在构建一个iOS应用,并且正在使用RestKit,Core Data和Objective Record。 Before I go into the details, note that my Object Context and Object Store for Objective Record and RKObjectManager are the same. 在详细介绍之前,请注意我的Objective Record的对象上下文和对象存储与RKObjectManager是相同的。 So I'm not referring objects outside the same contexts.Enough intro.. 所以我不是在相同上下文之外引用对象。

Feed_Items model is the top one in the hierarchy. Feed_Items模型是层次结构中的顶级模型。

--Feed_Items --Feed_Items

------OutFit Model (1-to-1 / Optional ) ------ OutFit模型(1对1 /可选)

------Clothe Model (1-t0-1 / Optional ) ------服装模型(1-t0-1 /可选)

Both Clothe and Outfit have same set of attributes and I'm interested particularly in the 'comments' attribute Clothe和Outfit具有相同的属性集,我对“ comments”属性特别感兴趣

--Clothe / Outfit -服装/服装

------Other fields ------其他领域

------NSSet* Comments object ------ NSSet *评论对象

When I start the app, I load all the latest feeds from the server using RestKit(json) and Object-Mapping. 启动应用程序时,我会使用RestKit(json)和对象映射从服务器加载所有最新的提要。 On the first load, I get only 1 feed item which is an outfit and that outfit has a total of 7 comments of which 5 are loaded initially. 第一次加载时,我仅获得一个饲料项,这是一套服装,该服装总共有7条评论,其中5条是最初加载的。 So far so good and I get the Feed_Item object and the corresponding Outfit and the 5 comments in NSSet. 到目前为止一切顺利,我在NSSet中得到了Feed_Item对象,相应的Outfit和5条注释。

Now when the user wants to load all the previous comments, I invoke another API and it returns 2 new comments which is parsed successfully as Comment objects. 现在,当用户想要加载所有先前的注释时,我调用另一个API,它返回2个新注释,这些注释已成功解析为Comment对象。

[objectManager getObjectsAtPath:[NSString stringWithFormat:@"api/outfit/comments/%d",[_feed.outfit.outfit_id intValue]]
             parameters:_params
                success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                    for( Comment* _comment in [mappingResult array]){
                        [_feed.outfit addCommentsObject:_comment];
                        [_comment save];
                    }
                }
                failure:^(RKObjectRequestOperation *operation, NSError *error) {

                }];

There aren't any exceptions or errors however I suspect the foreign key for the last 2 Comment objects are not getting updated in the sqlite db. 没有任何异常或错误,但是我怀疑最后两个Comment对象的外键没有在sqlite db中更新。 Please see the image below 请看下图

在此处输入图片说明 To further prove my point, I tried the below on my second run 为了进一步证明我的观点,我在第二次尝试中尝试了以下方法

Outfit *_outfit = [[Outfit where:@"outfit_id == 3"] objectAtIndex:0];
NSLog(@"%@", [_outfit.comments allObjects]);

and there are only 5 obects 而且只有5个怪兽

"<Comment: 0xc185ed0> (entity: Comment; id: 0xc185840 <x-coredata://2C61C98F-0B5F-4111-B681-1E43ABFB33D4/Comment/p6> ; data: <fault>)",
"<Comment: 0xc185c00> (entity: Comment; id: 0xc185820 <x-coredata://2C61C98F-0B5F-4111-B681-1E43ABFB33D4/Comment/p1> ; data: <fault>)",
"<Comment: 0xc185f10> (entity: Comment; id: 0xc185850 <x-coredata://2C61C98F-0B5F-4111-B681-1E43ABFB33D4/Comment/p8> ; data: <fault>)",
"<Comment: 0xc185f70> (entity: Comment; id: 0xc185860 <x-coredata://2C61C98F-0B5F-4111-B681-1E43ABFB33D4/Comment/p9> ; data: <fault>)",
"<Comment: 0xc185e90> (entity: Comment; id: 0xc185830 <x-coredata://2C61C98F-0B5F-4111-B681-1E43ABFB33D4/Comment/p4> ; data: <fault>)"

I don't know what happened to the last 2 objects and why it's not hooked up. 我不知道最后2个对象发生了什么,为什么不连接它。 It's successfully saved in the database as you can see from the image but looks like the relationship or the foreign key is not set. 正如您从图像中看到的那样,它已成功保存在数据库中,但看起来好像没有设置关系或外键。 Any idea what's going wrong? 知道发生了什么事吗?

Cheers 干杯

For those who are using RestKit + Core Data + Objective Record, please ensure you modify the saveTheContext method in NSManagedObject+ActiveRecord.m like the following. 对于使用RestKit + Core Data + Objective Record的用户,请确保您像下面这样修改NSManagedObject + ActiveRecord.m中的saveTheContext方法。

- (BOOL)saveTheContext {
    if (self.managedObjectContext == nil ||
        ![self.managedObjectContext hasChanges]) return YES;

    NSError *error = nil;
    BOOL save = [self.managedObjectContext saveToPersistentStore:&error];
    if (!save || error) {
        NSLog(@"Unresolved error in saving context for entity:\n%@!\nError: %@", self, error);
        return NO;
    }

    return YES;
} 

Key to note here is the call to saveToPersistentStore which is an RKAdditions category on NSManagedObjectContext. 这里要注意的关键是调用saveToPersistentStore,它是NSManagedObjectContext上的RKAdditions类别。 Without this RestKit initialized object won't save any changes in the persistent store. 没有此RestKit,初始化的对象将不会在持久性存储中保存任何更改。

It took me a day to figure this out. 我花了一天的时间才弄清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM