coolcool1994
coolcool1994

Reputation: 3812

addPersistentStoreWithType returns null (iOS, CoreData, iCloud)

I have this UIManagedDocument from CoreData that I am using correctly . . .

NSURL *localUrl = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
localUrl = [localUrl URLByAppendingPathComponent:@"Default Notes Database"];
//self.noteDatabase = [[UIManagedDocument alloc] initWithFileURL:localUrl]

Now I am trying to create NSPersistentStore instance in the process of trying to save Core Data in iCloud. But whenever I try to create NSPersistentStore using my data model and the url, NSPersistentStore is always nil.

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Notes" withExtension:@"momd"];
NSPersistentStoreCoordinator *migrationPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]];

// Open the store
id sourceStore = [migrationPSC addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:iCloudUrl options:@{ NSPersistentStoreUbiquitousContentNameKey: @"InhoNotesiCloudStore",NSSQLitePragmasOption: @{@"journal_mode":@"DELETE"},NSMigratePersistentStoresAutomaticallyOption:@YES,NSInferMappingModelAutomaticallyOption:@YES } error:nil];

SourceStore is nil! Why is this nil? I searched for answer, but found nothing online.

Upvotes: 0

Views: 135

Answers (1)

coolcool1994
coolcool1994

Reputation: 3812

The problem was the path to the store file was wrong - I found out debugging using simulator app files. If you create the CoreData with or without .sqlite extension, the store file end up being in different locations in the application's documents folder. In my case I didn't have .sqlite extension, which meant that my store files are located Documents/Default Notes Database/storeContents/persistentStore instead of Documents/Default Notes Database.sqlite

Upvotes: 0

Related Questions