ios – 核心数据对象变为空

我正在创建一个应该支持离线模式的任务应用程序.我已经使用RestKit下载任务并将其映射到本地Core数据.

这在线模式很好.但在离线时有奇怪的问题.我使用nspredicate从本地存储获取数据.为此,我使用的是Magical Records.

+ (void)getIdeasTasksWithPageNo:(int)pageNo completionHandler:(void (^)(NSArray *,NSError *))completionHandler {

    NSArray *tasks = [self MR_findAllWithPredicate:[nspredicate predicateWithFormat:@"due_date = nil AND user_id = %@",[DBUsers currentUser].id]];
    completionHandler(tasks,nil);
}

我称之为:

[DBTasks getIdeasTasksWithPageNo:1 completionHandler:^(NSArray *tasks,NSError *error) {
            if (!error) {
                [self displayTasksWithResults:tasks forPageNo:1];                   

            } else {
                NSLog(@"Error is %@",error);
            }
        }];

这是我在UITableView中显示的方式

-(void)displayTasksWithResults:(NSArray *)tasks forPageNo:(int)pageNo {
    if (!self.tasksArray) {
        self.tasksArray = [[NSMutableArray alloc] init];

    } else {
        [self.tasksArray removeAllObjects];
    }
    [self.tasksArray addobjectsFromArray:tasks];
    [self.tableview reloadData];
}

这只是第一次工作,所有任务都填充在UITableView中.

在填充UITableView之后的问题是,self.tasksArray中的所有记录都将变为Null.如果我滚动UITableView,表行开始为空.

但是如果我在displayTasksWithResults方法中打印self.tasksArray,它打印完美.

(
    "Title: Task 01","Title: You've gone incognito. Pages you view in incognito tabs won't stick around in your browser's history,cookie store,or search history after you've closed all of your incognito tabs. Any files you download or bookmarks you create will be kept. ","Title: Task 06","Title: Task 04","Title: Hi","Title: Task 3","Title: Task 4","Title: Hi 4","Title: hh","Title: Task 02","Title: Task 05\n","Title: Task 5","Title: Task 2 updated","Title: Here is a task. ","Title: Task 03","Title: Hi 3","Title: Task 2","Title: Hi 2","Title: Testing task email with Idea Task","Title: Task f6","Title: 1.117","Title: Task f5","Title: Task f12","Title: Task f4","Title: Task f3","Title: 111.0.113","Title: 111.0.115","Title: Pages you view in incognito tabs won't stick around in your browser's history,or search history after you've closed all of your incognito tabs. Any files you download or bookmarks you create will be kept.","Title: Task f7","Title: 1.116","Title: 1.118","Title: Going incognito doesn't hide your browsing from your employer,your internet service provider,or the websites you visit. ","Title: 111.0.111"
)

如果我以后打印self.taskArray,可能在UITableView的didSelectRow委托中,它打印如下:

(
    "Title: (null)","Title: (null)","Title: (null)"
)

我认为这可能是与NSManagedobjectContext相关的一些事情,但不知道如何解决它.

请帮忙!

解决方法

问题是(正如我在评论中写的)对象在后台线程上获取,但在主(UI)线程上使用.托管对象只能在上下文中“生活”
它们被创建.如果上下文被释放,对象仍然存在,但是
属性访问器方法只返回零.

可能的解决方案:

>在主线程上获取对象.
>使用

NSManagedobject *copy = [[mainContext objectWithID:[object objectID]];

将对象从背景上下文“复制”到主上下文. (也许MagicalRecord
一个方便的方法.)
>而不是提取被管对象,设置

[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:@[@"title",...]];

使用您感兴趣的属性获取一系列字典.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...