NSPrivateQueueConcurrencyType 没有正确保存 [英] NSPrivateQueueConcurrencyType Not saving properly

查看:24
本文介绍了NSPrivateQueueConcurrencyType 没有正确保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AFNetworking 从我的应用服务器获取信息后,将调用以下方法以填充我的 Core-Data.

The following method gets called in order to populate my Core-Data after AFNetworking fetches information from my app server.

信息似乎完美无缺,因为当表格更新时,我可以在 UITableView 中看到正在更新的新信息.

The information seems to be perfectly working as when the table is updated I can see the new information being updated in the UITableView.

现在我遇到的问题是即使我可以看到信息(从服务器获取信息后,存储到核心数据并重新获取以显示在我的UITableView中)如果我然后去关闭我的应用程序并重新打开它,信息不再存在.

Now the problem that I have is that even tho I can see the information ( after it has been fetches from the server, stored into Core-data and refetches to display in my UITableView) If I then go and close my app and re open it, the information is not there anymore.

似乎信息不是持久化的,问题似乎出在线程上.鉴于如果我在我的方法中删除线程选项一切正常.

It seems as if the information is not persistent and the problem seems to be the thread. given that if I remove the thread option in my method everything works fine.

我错过了什么??我已经尝试了我遇到的大多数事情,但似乎找不到解决方案.

What am I missing?? I have tried most things that I came across but I can't seem to find a solution.

NSManagedObjectContext *childContext = [[NSManagedObjectContext alloc]
                                        initWithConcurrencyType:NSPrivateQueueConcurrencyType];
childContext.parentContext = managedObjectContext;
myModel.context = childContext;


   [childContext performBlock:^{
// ... Lots Controller logic code that then calls the class myModel where all my Core-Data save methods are

    // Sort Wall Pictures
                    if ( [dataHolder[@"verb"] isEqualToString:@"addWallPicture"] ) {
                        data = @{  @"resourceID":dataHolder[@"_id"][@"$id"],
                                   @"resourceName":dataHolder[@"details"][@"resourceName"],
                                   @"author":@{ @"id":dataHolder[@"userId"][@"$id"],
                                                @"username":dataHolder[@"details"][@"authorName"] },
                                   @"likesNumber":@0,
                                   @"likesPeople":@[]
                                   };

                        [myModel saveSocialWall:data date:date verb:dataHolder[@"verb"] code:dataHolder[@"_id"][@"$id"] myUser:myUser];
                        continue;
                    }
[childContext save:&error];
}];

推荐答案

您必须在某些时候保存主上下文,例如保存子上下文后.

You have to save the main context as well at some point, e.g. after saving the child context.

保存子上下文只保存到主上下文,保存主上下文保存到存储文件.

Saving the child context saves only to the main context, and saving the main context saves to the store file.

像这样(写在手机上,会有是语法错误):

Like this (written on the phone, there will be syntax errors):

// ...
[childContext save:&error];
[mainContext performBlock:^{
    [mainContext save:&error];
}];

Swift 2.0 中:

do {
    try childContext.save()
    mainContext.performBlock { 
        do {
            try mainContext.save()
        } catch let err as NSError {
            print("Could not save main context: (err.localizedDescription)")
        }
    }
} catch let err as NSError {
    print("Could not save private context: (err.localizedDescription)")
}

这篇关于NSPrivateQueueConcurrencyType 没有正确保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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