【发布时间】:2014-04-29 06:33:00
【问题描述】:
我正在创建一个应该支持离线模式的任务应用程序。我已经使用 RestKit 下载任务并将其映射到本地核心数据中。
这在在线模式下运行良好。但是在离线时有一个奇怪的问题。我使用 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 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"
)
如果我稍后打印self.taskArray,可能在UITableView 的didSelectRow 代表中,它会打印如下:
(
"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)"
)
我认为这可能与NSManagedObjectContext 有关,但不知道如何解决。
请帮忙!
【问题讨论】:
-
当
tasks为空时,无论如何您都在清除tasksArray。有你的问题。 -
@Desdenova,感谢您的回复,清空任务数组永远不会被调用,它只会在新的数据请求时被调用。顺便说一句,我也尝试过评论清空任务数组的行,但仍然无法正常工作。
-
好的,显然有些东西正在清空数组。尝试在属性上放置一个断点,看看是什么在访问它。你应该可以从那里回溯。
-
对象是否在辅助托管对象上下文中获取?如果该上下文被释放,对象将继续存在,但其所有属性的计算结果为
nil。 -
@Desdenova,tasksArray 在任何地方都不是空的。 tasksArray.count 仍然显示 34。它是空的数据。这在在线模式下也很完美,对象由 RestKit 检索。
标签: ios iphone objective-c core-data restkit-0.20