2016-01-20 6 views
5

最初の軽量CoreDataの移行を試みています。軽量移行に関する2つのガイドを読んでいます。どちらも、NSPersistentStoreCoordinatorのような変数を変更し、追加し、CoreDataStackクラスにコードを追加します。SwiftでLightweight CoreDataの移行を行う方法

let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true, 
    NSInferMappingModelAutomaticallyOption: true] 

私の問題は、私はCoreDataを使って、完全な機能のアプリを持っているが、私は、そのクラスまたはそのようなものを持っていないということです。 私の問題はです。なぜこれらのプロジェクトは私がこのクラスを持っていると想定していますか?そうでない場合は、どうすれば追加できますか? 9月には

に答えるために必要な場合

詳細、私はCoreDataを使ってアプリを構築しました。 CoreDataを使用したのは初めてのことでした。このRay Wenderlichガイドに従っています。それは素晴らしい、私はアプリを終了し、今店で。今度は、新しいCoreDataアトリビュートといくつかの新しいエンティティを含む、アプリケーションのいくつかの変更を開始したいと思います。私は新しいモデルのバージョンをセットアップする必要があることを読んだ。

私はRay Wenderlich guideが見つかりましたが、それは私が持っていない、このCoreDataStack.swiftファイル使用しています:イライラ何

CoreData Class

は私のセットアップCoreDataがそのガイドを使用していることであり、それは含まれていませんでしたそのファイル!それから私はマイグレーションを行い、私はそれを持っていると仮定します。

私は、別の軽量移行方法を探しに行きました。このalternativeを発見し、私は私のCoreDataに組み込まれたことがない、それはあまりにも参照コード:

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = { 
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added 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. 
// Create the coordinator and store 
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) 
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("MyLog.sqlite") 
var error: NSError? = nil 
var failureReason = "There was an error creating or loading the application's saved data." 
if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil { 
    coordinator = nil 
    // Report any error we got. 
    var dict = [String: AnyObject]() 
    dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" 
    dict[NSLocalizedFailureReasonErrorKey] = failureReason 
    dict[NSUnderlyingErrorKey] = error 
    error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict) 
    // Replace this with code to handle the error appropriately. 
    // abort() 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. 
    NSLog("Unresolved error \(error), \(error!.userInfo)") 
    abort() 
} 

だから私はガイドを読んでの90%を理解してきましたチュートリアル。

let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true, 
    NSInferMappingModelAutomaticallyOption: true] 

答えて

8

移行オプションをする必要があります:私はちょうど私がCoreDataクラスを持っていない場合、私のような軽量のコードを追加することになり、その元CoreDataのチュートリアルを見て、どこを教えてくれる人が必要永続ストアを永続ストアコーディネータに追加したコールで使用されます。 addPersistentStoreWithTypeを検索することで、このコード行を簡単に見つけることができます。

try coordinator!.addPersistentStoreWithType(
    NSSQLiteStoreType, configuration: nil, URL: url, options: mOptions) 

は、ほとんどの場合、あなたのコアデータ・スタックは、AppDelegateクラスであるが、それがどこにある移行オプションを追加する必要がどこにかかわらず、これがあります。

+0

ありがとうございました!午前1時にそれを試し、それがうまくいけば正解をマークします。あなたはまた、私が言及したそのクラスが少し明るいこともあります。なぜ、そのチュートリアルでは、クラスによって処理される永続ストアですか? –

+0

ありがとうございます、これはうまくいきました。私はチュートリアルで見たコンソール出力を取得していないので、移行を示していますが、属性とエンティティを追加してからアプリを実行しても問題はありませんでした。 –

関連する問題