2017-08-28 5 views
0

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/InitializingtheCoreDataStack.html)ここでモデルクラスを使用してコアデータmanagedObjectContextを初期化する方法は? 。私は[コアデータスタックの初期化]コアデータ・スタックのためのアップルのドキュメント以下のい

は私のコードです:私はAppdelegateクラスから、そのオブジェクトにアクセスしようとしています

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)") 
     } 
    } 
} 
} 

: -

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() 
    } 
} 

致命的なエラーが発生しています。

理由はわかりませんが、saveValues()がコアデータ管理コンテキストオブジェクトの初期化の前に呼び出されている可能性があります。または私は間違った方法で初期化しているかもしれません。

私はAppleドキュメントに従っていますが、私は何が欠けているのか分かりません。誰でも私を提案できますか?

+0

コアデータの簡単なデモ更新削除を追加するhttps://github.com/SanjeetVerma/CoreData-CRUD-Operation-Add-Delete-Search- –

答えて

0

saveValuesは、completionClosureの外部で呼び出されます。これは致命的なエラーを受け取る方法です。コアデータスタックが準備されていません。

達成したいことを説明できますか?

+0

実際、コアデータを初期化したい私のモデルクラスからそれを保存し、更新して削除します。 ありがとう – kishor0011

+0

@ kishor0011は入力または初期化されますか? –

+0

私のモデルクラスで初期化します。 – kishor0011

関連する問題