title: When are NSManagedObjects deallocated?
body: <p>I have problem with deallocating my controller when I navigate back in hierarchy. I found that the problem would be with objects which are loaded from Core Data. When I comment out line with <code>// *</code> the controller is successfully deallocated but with line in place the <code>NSLog</code> isn't printed.</p>
  
  <p>I extract code from controller:</p>
  
  <pre><code>@implementation ModulesListViewController {
    NSArray *_modules;
  }
  
  - (void)viewDidLoad {
    [super viewDidLoad];
  
    _modules = [[StorageManager manager] getModulesCDByUserEmail:userEmail]; // *
    ...
  }
  
  - (void)dealloc {
    NSLog(@"ModulesListViewController dealloc");
  }
  </code></pre>
  
  <p><code>getModulesCDByUserEmail</code> is method which fetch data from Core Data and returns <code>NSArray</code>. Nothing unusual. I guess the problem is with <code>NSManagedObjectContext</code>.</p>
  
  <pre><code>- (NSManagedObjectContext *)managedObjectContext{
      if (_managedObjectContext != nil) {
          return _managedObjectContext;
      }
  
      _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
      _managedObjectContext.parentContext = [self writerManagedObjectContext];
  
      return _managedObjectContext;
  }
  </code></pre>
  
  <p>I have it in singleton and it's globally accessible so it's not deallocated. Is this a problem? Should I create own <code>NSManagedObjectContext</code> for each controller if I want to be properly deallocated? Could these contextes be <code>NSMainQueueConcurrencyType</code>? Or is there a way how can I deallocate objects fetched from context which isn't deallocated?</p>
  
  <p>Thanks for help</p>