2017-08-28 58 views
0

我下面的Apple文檔核心數據堆棧[初始化核心數據堆棧(https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/InitializingtheCoreDataStack.html如何使用模型類初始化核心數據managedObjectContext? 。

這裏是我的代碼:

import Foundation 
import CoreData 
class Cmodel: NSObject { 
var managedObjectContext: NSManagedObjectContext 

init(completionClosure: @escaping() ->()) { 
    //This resource is the same name as your xcdatamodeld contained in your project 
    guard let modelURL = Bundle.main.url(forResource: "MySampleListView", 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(contentsOf: modelURL) else { 
     fatalError("Error initializing mom from: \(modelURL)") 
    } 

    let psc = NSPersistentStoreCoordinator(managedObjectModel: mom) 

    managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.mainQueueConcurrencyType) 
    managedObjectContext.persistentStoreCoordinator = psc 

    let queue = DispatchQueue.global(qos: DispatchQoS.QoSClass.background) 
    queue.async { 
     guard let docURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last else { 
      fatalError("Unable to resolve document directory") 
     } 
     let storeURL = docURL.appendingPathComponent("DataModel.sqlite") 
     do { 
      try psc.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: storeURL, options: nil) 
      //The callback block is expected to complete the User Interface and therefore should be presented back on the main queue so that the user interface does not need to be concerned with which queue this call is coming from. 
      DispatchQueue.main.sync(execute: completionClosure) 
     } catch { 
      fatalError("Error migrating store: \(error)") 
     } 
    } 
} 
} 

我試圖從AppDelegate類訪問該對象: -

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    managedOBJ = Cmodel(completionClosure: { 

     DispatchQueue.main.async { 
      self.context = self.managedOBJ?.managedObjectContext 
     } 
    }) 

    saveValues() 
    // Override point for customization after application launch. 
    return true 
}. 

這就是我如何使用它。

//Save Local DB 
extension AppDelegate { 

    func saveValues() { 
    let testQuestionModel : Kishor = NSEntityDescription.insertNewObject(forEntityName: "Kishor", into: AppDelegate.getContext()) as! Kishor 

    testQuestionModel.name = "Kishor Da Pahalwani" 

    self.saveContext() 
    } 
} 

我得到致命錯誤。

我不知道原因,但可能會在Core Data託管上下文對象初始化之前調用saveValues()。或者可能是我以錯誤的方式初始化。

我正在嘗試關注Apple文檔,但我不知道自己錯過了什麼。請任何人都可以建議我?

+0

核心數據簡單演示添加更新刪除看看https://github.com/SanjeetVerma/CoreData-CRUD-Operation-Add-Edit-Delete-Search- –

回答

0

saveValues被稱爲completionClosure之外。這是你收到一個致命的錯誤。核心數據堆棧沒有準備好。

你能解釋一下你想達到什麼嗎?

+0

是啊其實我想初始化核心數據,我想保存並更新並從我的模型類中刪除它。 謝謝 – kishor0011

+0

@ kishor0011填充或初始化? –

+0

在我的模型類中初始化。 – kishor0011