NSManagedObjectContext 是否会存在足够长的时间来执行所有使用 -performBlock: 提交的块?

Will NSManagedObjectContext live long enough to perform all blocks submitted with -performBlock:?

考虑以下代码:

NSManagedObjectContext *parentContext = ... // global context, exists as long as app runs
MyEntity *parentEntity = ... // parentEntity is in parentContext

NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
context.parentContext = parentContext;

MyEntity *entity = [context objectWithID:parentEntity.objectID];

[context performBlock:^{
    // context is not referenced inside this block

    // Imagine entity is a fault, i.e. `entity.myStringProperty` is not cached.
    // Question: will context still be alive to fetch data from parent context?
    // i.e. does context live as long as its queue has pending blocks or block being run now?
    NSLog(@"%@", entity.myStringProperty);
}];

// context is no longer referenced by code, i.e. it may dealloc

context 还能活着从 parentContext 获取数据吗?即 context 是否只要它的队列有待处理的块或块现在是 运行 就存在?

简答:是。

您可以假设只要一个对象在做一些工作,它就会保存在内存中。上下文是一个功能齐全的上下文,应该能够按预期填充故障。