核心数据对象变为空

Kha*_*war 10 iphone core-data objective-c ios restkit-0.20

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

这在在线模式下运行良好.但在线下有一个奇怪的问题.我使用NSPredicate从本地存储中获取数据.为此我使用魔法记录.

+ (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);
}
Run Code Online (Sandbox Code Playgroud)

我称之为:

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

            } else {
                NSLog(@"Error is %@", error);
            }
        }];
Run Code Online (Sandbox Code Playgroud)

这就是我在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];
}
Run Code Online (Sandbox Code Playgroud)

这仅适用于第一次,并填充所有任务UITableView.

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

但如果我self.tasksArraydisplayTasksWithResults方法打印,它打印得很完美.

(
    "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 4",
    "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, 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 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"
)
Run Code Online (Sandbox Code Playgroud)

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

(
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)",
    "Title: (null)"
)
Run Code Online (Sandbox Code Playgroud)

我认为这可能与某些事情有关NSManagedObjectContext,但不知道如何解决它.

请帮忙!

Mar*_*n R 16

The problem is that (as I wrote in a comment) that the objects are fetched on a background thread, but used on the main (UI) thread. Managed objects can only "live" in the context that they were created in. If the context is deallocated, the objects still exist, but the property accessor methods return just nil.

Possible solutions:

  • Fetch the objects on the main thread.
  • Use

    NSManagedObject *copy = [[mainContext objectWithID:[object objectID]];
    
    Run Code Online (Sandbox Code Playgroud)

    to "copy" the objects from the background context to the main context. (Perhaps MagicalRecord has a convenience method.)

  • Instead of fetching managed objects, set

    [fetchRequest setResultType:NSDictionaryResultType];
    [fetchRequest setPropertiesToFetch:@[@"title", ...]];
    
    Run Code Online (Sandbox Code Playgroud)

    to fetch an array of dictionaries with the attributes you are interested in.