3

我的 UITableViewCell 的部分内容创建因一个对象(CoreData NSManagedObject)初始访问时发生的故障而延迟。这表现为一个小问题,首先将单元格滚动到视图中。我决定将这些对象的访问权推到后台线程。

这就是我实现它的方式并且效果很好,但是我们都知道我们不应该在另一个线程中访问一个线程(主线程)的 NSManagedObjectContext,但是如果我们可以在第二个线程中获取对象的 objectID它最初是在第一个线程中获取的?

获取 objectID 需要花费少量时间,我希望将其与其他所有内容一起推到后台。

MyRecord *record = [self.frc objectAtIndexPath: indexPath];

// Should the following be here or can it be below in the background thread?
// NSManagedObjectID *recordObjectID = record.objectID;

dispatch_async(_recordViewQueue, ^(void) {
    if ([cell.origIndexPath isEqual:indexPath]) {

        // should the following be here or above?  It works here, but am I just lucky?
        // this call seems to take about 2/100 of a second
        NSManagedObjectID *recordObjectID = record.objectID;

        NSManagedObjectContext *bgndContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];
        bgndContext.persistentStoreCoordinator = App.sharedApp.storeCoordinator;
        MyRecord *newRecord = (MyRecord *) [bgndContext objectWithID:recordObjectID];

        [self updateCell:cell withRecord:newRecord];

        if ([cell.origIndexPath isEqual:indexPath]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [(UIView*) cell.recordView setNeedsDisplay];
            });
        }
    }
});

这安全吗?还是我必须在 mainThread 中获取 objectID?

4

1 回答 1

5

在线程之间传递托管对象的 objectID 是安全的。在线程之间使用托管对象是不安全的。使用 objectID 和线程的托管对象上下文来调用 existingObjectWithID:error: 以获取该线程的托管对象的实例。

我会像这样更新您的代码:

MyRecord *record = [self.frc objectAtIndexPath: indexPath];

NSManagedObjectID *recordObjectID = record.objectID;

dispatch_async(_recordViewQueue, ^(void) {
    if ([cell.origIndexPath isEqual:indexPath]) {

        NSManagedObjectContext *bgndContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];
        bgndContext.persistentStoreCoordinator = App.sharedApp.storeCoordinator;
        NSError * error = nil;
        MyRecord *newRecord = (MyRecord *) [bgndContext existingObjectWithID:recordObjectID error:&error];
        if (newRecord) {
            [self updateCell:cell withRecord:newRecord];
            if ([cell.origIndexPath isEqual:indexPath]) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [(UIView*) cell.recordView setNeedsDisplay];
                });
            }
        }
        else {
            NSLog(@"unable to find existing object! error: %@ (userInfo: %@)", [error localizedDescription], [error userInfo]);
        }
    }
});
于 2013-07-30T22:17:07.967 回答