0

Consider following code:

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

Will context still be alive to fetch data from parentContext? i.e. does context live as long as its queue has pending blocks or block being run now?

Mundi
  • 79,884
  • 17
  • 117
  • 140
Oleksii Taran
  • 348
  • 3
  • 10

1 Answers1

0

Short answer: yes.

You can assume that as long as an object is doing some work it will be kept in memory. The context is a fully functioning context that should be able to fill faults as expected.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • is it documented somewhere, or is it your experience? I feel like this would be a natural thing for context to live long enough, but I want to be sure because otherwise it will lead to bugs that are hard to reproduce. – Oleksii Taran Aug 20 '15 at 00:06
  • 1
    It is my experience; and I think it would be unlikely to be documented. It seems obvious that an active thread belonging to an object cannot allow the object to be released. – Mundi Aug 20 '15 at 00:11