ios - 核心数据对象变为空

标签 ios iphone objective-c core-data restkit-0.20

我正在创建一个应支持离线模式的任务应用程序。我使用 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,可能在 UITableViewdidSelectRow 委托(delegate)中,它打印如下:

(
    "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 相关,但不知道如何解决。

请帮忙!

最佳答案

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

可能的解决方案:

  • 在主线程上获取对象。
  • 使用

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

    将对象从后台上下文“复制”到主上下文。 (也许是 MagicalRecord 有一个方便的方法。)

  • 不是获取托管对象,而是设置

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

    获取包含您感兴趣的属性的字典数组。

关于ios - 核心数据对象变为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23357195/

相关文章:

ios - 如何在屏幕上显示模拟器键盘时使用 Mac 键盘

android - 在 Delphi Firemonkey 移动应用程序中读写 inifile?

iphone - 检查当前 viewController 类是否为 IOS 7 中的 Person ViewController 时出现问题

iphone - 如何在 UIWebView 中设置内容偏移量和内容大小

ios - 重用 UITableViewCell 与不同的数据

iphone - 检测移动的 UIImageView 是否被触摸

objective-c - chatDidReceiveMessage 方法未调用 QuickBlox

ios - 如何在 iOS 中让线程休眠几秒钟?

ios - CoreData写对象慢

iOS - 带摘要的网络调用