【问题标题】:Executing fetch requests in the background using MagicalRecord使用 MagicalRecord 在后台执行获取请求
【发布时间】:2014-04-01 19:16:16
【问题描述】:

这是我目前所拥有的:

NSManagedObjectContext *privateContext = [NSManagedObjectContext MR_context];
[privateContext performBlock:^{
        Cat* object = [Cat MR_findFirstByAttribute:@"id" withValue:catId inContext:privateContext];
        // Convert your fetched objects into object IDs which can be pulled out of the main context
        NSString *privateObjectID = [object valueForKey:@"objectID"];

        // Return to our main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            ???
        });
    }];

这是正确的做法吗?如果是这样,我如何让我的对象回到主线程? (上面的???区域)

如果我这样做:

NSManagedObjectContext* context = [NSManagedObjectContext MR_contextForCurrentThread];
Cat* object = [Cat MR_findFirstByAttribute:@"id" withValue:catId inContext:context];

UI 被阻塞,所以我试图弄清楚如何在不阻塞主线程的情况下进行提取。

【问题讨论】:

    标签: ios core-data nsmanagedobjectcontext magicalrecord


    【解决方案1】:

    块内的代码(在主线程上执行的代码)应该是:

    NSManagedObjectContext* c = [NSManagedObjectContext MR_contextForCurrentThread];
    Cat* o = (Cat*)[context objectWithID:object.objectID];
    //access `o` so it will get faulted in by the main context
    //I think you could use: [o valueForKey:nil] or [o didAccessValueForKey:nil]
    

    重要的是在从主上下文访问它们时将 BG 获取的对象保留在(协调器)缓存中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-29
      • 1970-01-01
      相关资源
      最近更新 更多