2015-10-25 44 views
13

我下面這個教程完全相同,這增加CoreData到現有的應用程序: https://www.youtube.com/watch?v=WcQkBYu86h8如何解決:「NSPersistentStoreCoordinator沒有持久存儲」?

當我到了seedPerson()moc.save(),應用程序崩潰與此錯誤:

CoreData: error: Illegal attempt to save to a file that was never opened. "This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation.". No last error recorded.

已添加NSManagedSubclass。

DataController已連線,我可以進入它。直到save()出現錯誤。任何想法可能會導致這個錯誤?

+0

也許你忽略了對'addPersistentStoreWithType:configuration:URL:options:error:'的調用?在將數據保存到它之前,您必須添加持久性存儲。 –

+0

發佈創建核心數據堆棧的代碼。 –

回答

2

不幸的是,該視頻使用了蘋果網站的一些代碼,並且該代碼示例存在缺陷。其主要缺陷是在持久性商店被添加到商務部之前它會緩存MOC。因此,如果商店的創建完全失敗,則管理對象上下文將使用沒有商店的持久性商店協調器進行初始化。

您需要使用調試器並逐步完成創建PSC(DataController.init方法)的代碼並查看失敗發生的原因。如果您在該示例中剪切/粘貼的方式相同,那麼也許您在實例化模型時忘記更改模型的名稱。

在任何情況下,最可能的原因是該函數中的某些初始化代碼失敗,隨後您將愉快地與沒有存儲的核心數據堆棧一起。

14

我也跟着YouTube的教程,並有同樣的問題。我剛剛刪除了添加持久性存儲的後臺線程塊,它工作。這裏是我的DataController

import UIKit 
import CoreData 

class WellbetDataController: NSObject { 
    var managedObjectContext: NSManagedObjectContext 

    override init() { 
     // This resource is the same name as your xcdatamodeld contained in your project. 
     guard let modelURL = NSBundle.mainBundle().URLForResource("DataModel", withExtension:"momd") else { 
      fatalError("Error loading model from bundle") 
     } 

     // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model. 
     guard let mom = NSManagedObjectModel(contentsOfURL: modelURL) else { 
      fatalError("Error initializing mom from: \(modelURL)") 
     } 

     let psc = NSPersistentStoreCoordinator(managedObjectModel: mom) 
     self.managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType) 
     self.managedObjectContext.persistentStoreCoordinator = psc 

     let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) 
     let docURL = urls[urls.endIndex-1] 
     /* The directory the application uses to store the Core Data store file. 
     This code uses a file named "DataModel.sqlite" in the application's documents directory. 
     */ 
     let storeURL = docURL.URLByAppendingPathComponent("DataModel.sqlite") 
     do { 
      try psc.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil) 
     } catch { 
      fatalError("Error migrating store: \(error)") 
     } 



//  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) { 
//   let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) 
//   let docURL = urls[urls.endIndex-1] 
//   /* The directory the application uses to store the Core Data store file. 
//   This code uses a file named "DataModel.sqlite" in the application's documents directory. 
//   */ 
//   let storeURL = docURL.URLByAppendingPathComponent("DataModel.sqlite") 
//   do { 
//    try psc.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil) 
//   } catch { 
//    fatalError("Error migrating store: \(error)") 
//   } 
//  } 
    } 
} 
+1

謝謝!進入相同的問題 - 刪除'dispatch_async'修復它。 –

+0

我遇到了一個類似的問題,使用書中的一些示例代碼。我試圖在應用程序啓動時爲測試目的設置一些初始託管對象。將此操作移回主線程意味着NSPersistentStoreCoordinator設置在其他任何事情都有機會嘗試將數據寫入NSManagedObjectContext之前完成。 在我的情況下,我想我在嘗試在異步操作中完成安裝工作之前寫入NSManagedObjectContext。 –

0

的問題是這兩行:

guard let modelURL = NSBundle.mainBundle().URLForResource("DataModel", withExtension:"momd") else { 

& &

let storeURL = docURL.URLByAppendingPathComponent("DataModel.sqlite") 

DataModel需要改變你的應用程序的名稱,如果你的CoreData是由Xcode自動創建。尋找這些行AppDelegate.swift

0

如果這是你,你把核心數據之後,可以那麼也許可以通過從模擬器的應用工作,並再次運行它運行應用程序的第一次。

它發生在我身上,它在我做到這些之後起作用。

相關問題