既存のリリース済みのアプリのCoreDataのデータを取り出したいです。
##経緯について
swift 2.x で実装されているアプリがあり、改修が難しいため、
プロジェクトを新しく作り直してリリースしようと思っております、
ただ、Coredataを使ってローカルにデータが保存されていまして、新しいプロジェクトでこのデータを読み出したいです。
Bundle Identifireは同じにして、同じアプリのバージョンアップのようにして見せたいのですが古いデータが読み出せないという状況です。
##試したこと
xcdatamodeldファイルををそのまま新しいプロジェクト(新PJ)にコピーして読み出せば良いのかと思いましたが読み出せませんでした。
保存も読み出しもできるのですが新PJで保存したものは新PJで読み出せます。旧PJで保存したものは旧PJのみで読み出せる状況でして
モデルは同じままDBが複数あるような状況になっています。
Bundle Identifireが同じでもこのようなことはできないのでしょうか。
環境
swift 2.3 -> swift 4
OS: iOS
swift 2.3の persistentStoreCoordinator
lazy var managedObjectModel: NSManagedObjectModel = { // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model. let modelURL = NSBundle.mainBundle().URLForResource("db", withExtension: "momd")! return NSManagedObjectModel(contentsOfURL: modelURL)! }() lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = { // The persistent store coordinator for the application. This implementation creates and returns 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. let options = [ NSInferMappingModelAutomaticallyOption: true, NSMigratePersistentStoresAutomaticallyOption: true ] // Create the coordinator and store let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("CoreData.sqlite") var failureReason = "There was an error creating or loading the application's saved data." do { //try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil) try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: options) } catch { // 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 as NSError let wrappedError = 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 (wrappedError), (wrappedError.userInfo)") abort() } return coordinator }()
回答1件
あなたの回答
tips
プレビュー