aahrens
aahrens

Reputation: 5590

Loading Core Data From Large JSON Causing App To Crash

I'm attempting to populate CoreData from a JSON file that consists of 170,000 plus dictionaries. The parsing of the json goes quick but when I start trying to add to CoreData I'm blocking the main thread for a long time and then the app eventually crashes. It crashes when calling the method [UIDocument saveToUrl:forSaveOperation:completionHandler] Here is my code. If anyone has an idea of what's causing it to crash or a more efficient way to load CoreData that would be greatly appreciated.

@property (nonatomic, strong) UIManagedDocument *wordDatabase;

- (void)viewWillAppear:(BOOL)animated
  {
    [super viewWillAppear:animated];
    if (!self.wordDatabase) {
      NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
      url = [url URLByAppendingPathComponent:@"Word Database"];
      self.wordDatabase = [[UIManagedDocument alloc] initWithFileURL:url];
    }
 }

- (void)setWordDatabase:(UIManagedDocument *)wordDatabase
  {
    if (_wordDatabase != wordDatabase) {
      _wordDatabase = wordDatabase;
      [self useDocument];
    }
  }

- (void)useDocument
  {
    if (![[NSFileManager defaultManager] fileExistsAtPath:[self.wordDatabase.fileURL path]]) {
      // does not exist on disk, so create it
      [self.wordDatabase saveToURL:self.wordDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
          [self setupFetchedResultsController];
          [self prepopulateWordDatabaseWithDocument:self.wordDatabase];
       }];
     }
  }

- (void)prepopulateWordDatabaseWithDocument:(UIManagedDocument *)document
  {
    dispatch_queue_t fetchQ = dispatch_queue_create("Word Fetcher", NULL);
    dispatch_async(fetchQ, ^{
    //Fetch the words from the json file
    NSString *fileString = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"json"];
    NSString *jsonString = [[NSString alloc] initWithContentsOfFile:fileString encoding:NSUTF8StringEncoding error: NULL];
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    NSArray *words = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    [document.managedObjectContext performBlock:^{
        for (NSDictionary *dictionary in words)
        {
            [Word wordFromDictionary:dictionary inManagedObjectContext:document.managedObjectContext];
        }

        [document saveToURL:document.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:NULL];
      }];
    });

   dispatch_release(fetchQ);
 }

Upvotes: 1

Views: 285

Answers (1)

aahrens
aahrens

Reputation: 5590

What I ended up doing that stopped my app from crashing was allocating a new NSManagedObjectContext and peformed all my loading in the background. After saving I called my NSFetchedResultsController and the table repopulated.

- (void)prepopulateWordDatabaseWithDocument:(UIManagedDocument *)document
 {
     NSManagedObjectContext *backgroundContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
     backgroundContext.undoManager = nil;
     backgroundContext.persistentStoreCoordinator = document.managedObjectContext.persistentStoreCoordinator;
     [backgroundContext performBlock:^{
         NSString *fileString = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"json"];
         NSString *jsonString = [[NSString alloc] initWithContentsOfFile:fileString encoding:NSUTF8StringEncoding error: NULL];
         NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
         NSError *parseError;
         NSArray *words = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&parseError];

         for (NSDictionary *dictionary in words)
         {
             [Word wordFromDictionary:dictionary inManagedObjectContext:backgroundContext];
         }

         NSError *loadError;
         if ([backgroundContext save:&loadError]) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 [self setupFetchedResultsController];
             });
         }
     }];
 }

Upvotes: 1

Related Questions