Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone - coredata application directory

I am using this Xcode template for core data.

On the delegate, I have this method:

- (NSURL *)applicationDocumentsDirectory {
  return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

but this requires iOS4.

I am trying to make the app compatible with 3.0.

So, I have converted this method to:

- (NSURL *)applicationDocumentsDirectory {

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  return [NSURL URLWithString:documentsDirectory];

}

but when coredata tries to use the store URL on

  NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyFile.sqlite"];
  if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

it crashes on the IF line with the message:

CoreData SQL stores only support file URLs (got /var/mobile/Applications/BB312A7E-6AE1-4BA2-AD87-6B96D8855CC6/Documents/MyFile.sqlite).'

but StoreURL is a NSURL!

Any clues? Thanks

like image 690
Duck Avatar asked Jan 14 '11 21:01

Duck


2 Answers

Try doing:

NSString *pathToYourSqliteFile = @"/your/path/here";
NSURL *storeURL = [NSURL fileURLWithPath: isDirectory:NO];

See if that does the trick.

like image 123
Dan K. Avatar answered Nov 16 '22 13:11

Dan K.


See CoreData application directory crashes on iOS3

The suggestion from Surfdev works ok on 3.1.2

like image 30
Olaf Avatar answered Nov 16 '22 14:11

Olaf