NSPrivateQueueConcurrencyType未正确保存 [英] NSPrivateQueueConcurrencyType Not saving properly

查看:124
本文介绍了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天全站免登陆