title: How to correctly initialize, use and save a UIManagedDocument
body: <p>I'm dealing to get my app work using a UIManagedDocument. I posted another question few days ago, but I haven't gotten it working yet. Let's see if I can do this better.<br>
  My app needs to load some data from a plist file and fill the database with it. Then, there are some parameters that will be changing now and again, and I also need to load and update them in the database.<br>
  The app should follow this sequence: Create/Open the database, load the data, update the variable data.<br>
  My problem comes when trying to follow that sequence correctly, mainly because I can't update the variable data before all objects are created in the database (and so the nil variables that have to be updated), and my workarounds only lead me to unexpected crashes and illogical behaviours.<br>
  Here's my code so far:  </p>
  
  <p>In the view code:  </p>
  
  <pre><code>- (void)viewDidLoad{
      [super viewDidLoad];
      self.database = [DataHelper opendatabaseAndUseBlock:^{
          [self setupFetchedResultsController]; // I pass this method trough a Block because I want it
                                                // to be executed once the database is opened, not before.
      }];
  }  
  </code></pre>
  
  <p>Then I use this helper class "DataHelper" which contains the following code:</p>
  
  <pre><code>@implementation DataHelper
  
  // This method creates and opens the database, then calls the necessary methods
  + (UIManagedDocument *)openDatabaseAndUseBlock:(completion_block_t)completionBlock
  {
  
      NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
      url = [url URLByAppendingPathComponent:@"Database"];
      UIManagedDocument *database = [[UIManagedDocument alloc] initWithFileURL:url];
  
      if (![[NSFileManager defaultManager] fileExistsAtPath:[database.fileURL path]]) {
          [database saveToURL:database.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
              [self loadDataIntodatabase:database withSuccess:success];
              completionBlock();
          }];
  
      } else if (database.documentState == UIDocumentStateClosed) {
          [database openWithCompletionHandler:^(BOOL success) {
              [self loadDataIntodatabase:database withSuccess:success];
              completionBlock();
          }];
  
      } else if (database.documentState == UIDocumentStateNormal) {
          [self loadDataIntodatabase:database withSuccess:YES];
          completionBlock();
      }
  
      return database;
  }
  
  
  // This method loads the "static" data into the database, by reading it from a plist file.
  // Once the loading finishes, it should call the last method, for updating the variable data
  + (void)loadDataIntoDatabase:(UIManagedDocument *)database withSuccess:(BOOL)success
  {
      if (success) {        
          NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
          NSArray *plistData = [NSArray arrayWithContentsOfFile:path];
  
          [database.managedObjectContext performBlock:^{
              for (NSDictionary *data in plistData) {
                  [Object createObjectWithData:data inManagedObjectContext:database.managedObjectContext];
              }
  
              // Not sure what to do here!!
              //[database saveToURL:database.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success) {
                   // [DataHelper updateVariableDataOfDatabase:database];
              //}];
  
              // Or...?
              //[database updateChangeCount:UIDocumentChangeDone];             
              // [DataHelper updateVariableDataOfDatabase:database];
  
          }];
  
      } else {
          NSLog(@"NO SUCCESS");
      }
  }
  
  // This last method updates some parameters of the existing data
  // in the database, to the ones found in another plist file
  + (void)updateVariableDataOfDatabase:(UIManagedDocument *)database
  {
      // This path is provisional, should be gotten from an online resource
      NSString *path = [[NSBundle mainBundle] pathForResource:@"VariableData" ofType:@"plist"];
      NSDictionary *variables = [NSDictionary dictionaryWithContentsOfFile:path];
  
      [database.managedObjectContext performBlock:^{
          // Update the objects changing the values of the variable parameters
          // to the ones found in "variables"
  
          // Now I should save the changes, or maybe not?
          //[database saveToURL:database.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:nil];
          // Or...
          //[database updateChangeCount:UIDocumentChangeDone];
  
      }];
  }
  @end
  </code></pre>
  
  <p>If I use the "saveToURL" method, although it seems to not be correct, it does the loading sequence correctly, because doesn't execute the last method until all the data has been loaded into the database. However, the app crashes randomly when trying to do many operations (with lots of savings inside, like deletion, updating or reloading).<br>
  On the other hand, by using "updateChangeCount", the app doesn't crash anymore, but the sequence doesn't work well: the variable data doesn't get updated, the app doesn't find an object that is there, on the database, and so on. If I wait 2 minutes or so between each action, then yes, it works...but that's not what I want.  </p>
  
  <p>I'd really appreciate some help here, this is consuming my time, and the deadline of this project is quite near :(<br>
  Thanks a lot!</p>