Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

persistentstorecoordinator sqlite error code:522 'not an error'

I'm working on an iOS project that deals with migration using different versions of my coredata.

I also tried surrounding the if statement in a catch and it returns a sqlite error code 522.

Is there anything wrong here?

my following code:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator{
  if (__persistentStoreCoordinator != nil) {
    return __persistentStoreCoordinator;
  }
  NSURL *storeURL = [[self applicationDocumentsDirectory]     
  URLByAppendingPathComponent:@"coredatadb.sqlite"];
  NSError *error = nil;
  __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

  NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
  [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
  [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

  if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]){

      [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];
      [__persistentStoreCoordinator release];
      __persistentStoreCoordinator = nil;
      return [self persistentStoreCoordinator];
  }

  return __persistentStoreCoordinator;
like image 770
kevinl Avatar asked Aug 16 '13 15:08

kevinl


2 Answers

By changing the journaling mode you only avoid the problem but you do not fix it. The new journaling mode gives your app some benefits like speed.

The real problem is you delete only 1 file instead of all 3 that are used in that mode. There's not only your coredatadb.sqlite but also an coredatadb.sqlite-shm and and coredatadb.sqlite-wal file. Something about schema and write-ahead or so, check the WWDC 2013 videos on Core Data for details. [Update: It starts at minute 40 in the https://developer.apple.com/videos/play/wwdc2013/207/ video]

To fix your problem, you should delete all files in the Documents directory that begin with "coredatadb.sqlite" and everything is free and easy again ;-)

Update for iOS 9: It is easier and safer now to use destroyPersistentStoreAtURL or replacePersistentStoreAtURL. See WWDC 2015 session 220_hd_whats_new_in_core_data.

like image 156
MacMark Avatar answered Oct 19 '22 23:10

MacMark


So it looks like the following solved my specific issue, although some people will advise against changing the journaling mode on your sqlite database:

// Change journal mode from WAL to MEMORY
NSDictionary *pragmaOptions = [NSDictionary dictionaryWithObject:@"MEMORY" forKey:@"journal_mode"];

 NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
 pragmaOptions, NSSQLitePragmasOption, nil];
like image 21
kevinl Avatar answered Oct 20 '22 01:10

kevinl