2016-10-18 57 views
0

我從CoreData中正確使用了此UIManagedDocument。 。 。addPersistentStoreWithType返回null(iOS,CoreData,iCloud)

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

現在我想在試圖保存核心數據在iCloud中的過程中創造NSPersistentStore實例。但是每當我嘗試使用我的數據模型和url創建NSPersistentStore時,NSPersistentStore始終爲零。

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!爲什麼這個零?我搜索了答案,但沒有在網上發現任何內容。

回答

0

問題是存儲文件的路徑錯誤 - 我發現使用模擬器應用程序文件進行調試。如果您使用或不使用.sqlite擴展名創建CoreData,則存儲文件最終會位於應用程序文檔文件夾中的不同位置。在我的情況下,我沒有.sqlite擴展名,這意味着我的商店文件位於Documents/Default Notes Database/storeContents/persistentStore而不是Documents/Default Notes Database.sqlite