2016-10-07 15 views
0

私のアプリでは、データをダウンロードしてCoreDataに保存するタスクがあります。私はswift 3に移行した後、ランダムな時間にデータを保存しながら例外をスローし始めました。私が理解しているように、私はすべての操作に1つのコンテキストを使用すると起こります。今私は、並行処理のタイプと、別のコンテキストを作成し、すべてがエラーなしで動作しますが、何もファイルを.sqliteに保存されていない:) ここで私は、コンテキストの作成方法は次のとおりです。ConcerrencyTypeを持つNSManagedObjectContextをswift 3に追加する方法は?

static let context : NSManagedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 
static let privateContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.privateQueueConcurrencyType) 

static func declare() 
{ 
    AixmParser.privateContext.parent = AixmParser.context 
} 

私はこのようなデータを保存します。

do{ 
     try privateContext.save() 
    } 
    catch{ 
    } 

宣言またはコアデータスタックに何かを追加する必要がありますか?

更新:以下のコアデータスタック。

// MARK: - Core Data stack 

lazy var persistentContainer: NSPersistentContainer = { 
    /* 
    The persistent container for the application. This implementation 
    creates and returns a container, having loaded the store for the 
    application to it. This property is optional since there are legitimate 
    error conditions that could cause the creation of the store to fail. 
    */ 
    let container = NSPersistentContainer(name: "AppName") 
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in 
     if let error = error as NSError? { 
      // Replace this implementation with code to handle the error appropriately. 
      // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 

      /* 
      Typical reasons for an error here include: 
      * The parent directory does not exist, cannot be created, or disallows writing. 
      * The persistent store is not accessible, due to permissions or data protection when the device is locked. 
      * The device is out of space. 
      * The store could not be migrated to the current model version. 
      Check the error message to determine what the actual problem was. 
      */ 
      fatalError("Unresolved error \(error), \(error.userInfo)") 
     } 
     container.viewContext.perform({ 
      // actions upon the NSMainQueueConcurrencyType NSManagedObjectContext for this container 
     }) 

    }) 
    return container 
}() 

// MARK: - Core Data Saving support 

func saveContext() { 
    let context = persistentContainer.viewContext 
    if context.hasChanges { 
     do { 
      try context.save() 
     } catch { 
      // Replace this implementation with code to handle the error appropriately. 
      // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
      let nserror = error as NSError 
      fatalError("Unresolved error \(nserror), \(nserror.userInfo)") 
     } 
    } 
} 
+0

コアデータスタックをどのようにセットアップしたかを確認する必要があります。 'save()'は値を保存し、 'parentContext'にプッシュします。 'save()'を 'while'ループ内で、親を持たない最後のコンテキストに連鎖させる必要があります。このコンテキストは 'persistentStoreCoordinator'に直接接続されています。 –

+0

@ New16、私は私のコアデータスタックを追加しました。 'save()'をどのように連鎖するべきですか? – Jamil

答えて

1

だから、最初は、あなたがprivateContextからNSPersistentContainerviewContextへのリンクを作成する必要があることです。プライベートを作成する際には、 privateContextprivateContext.parentContext = viewContextとなります。保存中 let context = privateContext while(context != nil) { context.save() context = context.privateContext } これは動作します。 なぜ最初にエラーが発生したのか分かりません。

+0

それは同じではありませんか?私はviewContextであるコンテキストを作成し、それをprivateContextのparentContextとして設定します。 保存について - コアデータスタックに追加するか、どこにでも保存コードを置き換える必要がありますか? – Jamil

+0

'NSManagedObjectContext'の' extension'を作成して、 'save()'メソッドの伝播をインクルードします。だから、 'saveAndPropagate()'のように 'save()'を呼び出す代わりに。 –

+0

'context = context.privateContext' ここで' context.parent'を意味しましたか? – Jamil

関連する問題