title: iOS Core Data when to save context?
body: <p>I'm having random crashes with core data due to concurrency and multithreading. I know that core data is not thread safe. I also found a couple other answers on how to create a ThreadedDataService and instantiate separate context for each thread. </p>
  
  <p>This is a bit too much for me to swallow at the moment, so I'm trying to find an easier way out.</p>
  
  <p>The solution that I'm trying at the moment is simple: <strong>saving data through the main thread.</strong> However, now a new issue arises: <strong>deadlock</strong>. The app becomes unresponsive because each of my insertions of a new NSManagedObject is followed by a call to save. (that's my best guess).</p>
  
  <p>Reading the App Delegate documentation, I noticed that it advises me to save context in applicationWillTerminate.</p>
  
  <p>My question is this: <strong>For a long running operation that inserts new events every minute, and the user is not required to see updates propagated to all controllers immediately, when is it a good time for me to save the context?</strong> 
  I'm getting a feeling that saving context for each record may be an overkill?</p>
  
  <pre><code>-(void)insertNewEvent
  {
  
  
      // Create a new instance of the entity managed by the fetched results controller.
      NSManagedObjectContext *context = [self.fetchedResultsController.managedObjectContext];
      NSEntityDescription *entity = [[self.fetchedResultsControllerfetchRequest] entity];
      Event*newManagedObject = (Event*)[NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
  
  //use accessor methods to set default values
  
      // Save the context. &gt; IS THIS REALLY NEEDED HERE?
      NSError *error = nil;
      if (![context save:&amp;error])
      {
  
  
      }else
      {
          if(newManagedObject!=nil)
          {
              currentState= newManagedObject;
              [currentRecord addEvent:newManagedObject];
  //Is this call needed?
              [self saveApplicationRecords];      
          }
      }
  
  }
  </code></pre>
  
  <p>I have methods like these defined for all of my managed objects, is it enough if I call such method on a main thread every 10-15 minutes to save pending changes, rather than doing so after each record insertion?</p>
  
  <pre><code>-(void)saveApplicationRecords
  {
       NSLog(@"saveApplicationRecords");
      NSManagedObjectContext *context = [self.applicationRecordsController.managedObjectContext];
      // Save the context.
      NSError *error = nil;
      if (![context save:&amp;error])
      {
          NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  
      }
  
  }
  </code></pre>
  
  <p><strong>An extra question after reading macbirdie's response: is this kind of method legal in core data?</strong></p>
  
  <pre><code>-(Event*)insertAndReturnNewEventWithDate:(NSDate*)date_ type:(int)type
  {
   NSManagedObjectContext *context = [self.dreamEventsController managedObjectContext];
      NSEntityDescription *entity = [[self.dreamEventsController fetchRequest] entity];
      DreamEvent *newManagedObject = (Event*)[NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
  
  //handle properties
  
   NSError *error = nil;
      if (![context save:&amp;error])
      {
  return nil;
  
      }else
      {
  return newManagedObject ;
  }
  
  }
  </code></pre>
  
  <p>Thank you!</p>