如何将Core Data添加到现有的Xcode 9 Swift 4 iOS 11项目中?

时间:2018-02-10 18:10:00

标签: ios xcode core-data

请求更新,因为此问题当然已针对先前版本得到解答,最新的搜索结果日期为12月16日,与之前的iOS 9和10项目产生无关的兼容性。

文档当然是说在开始一个新项目时选择使用核心数据复选框,我没有选择,但现在认为需要添加iCloud +核心数据以使我的应用程序进入下一阶段 - >其中需要NSFileCoordinator和NSFilePresenter之类的东西,因为在我的app UI中,用户会看到一些TOPICS,每个都有三个OPTIONS,用户可以选择一个选项。对于每个主题,UI然后显示已选择每个选项的总用户数和每个选项的总数的PERCENTAGE。

现在,每个选项的选择数量和总数的百分比当然只是在我的原生应用中计算的 - >但实际上需要在像云一样的中心或最有可能在网站上进行计算...但是网站引发了NSFileCoordinator和NSFilePresenter已经解决的同时读/写问题。

因此,如果iCloud +核心数据系统可以在收到来自各个用户的写入数值命令之后插入现有的普遍容器数值总计的基本算术计算 - 在发送新的无所不在的容器数值总和百分比值之前 - 那么我非常感谢建议在尝试创建和初始化核心数据堆栈时如何修复下面生成的错误。否则猜测我将不得不刮掉Xcode并转到像PhoneGap这样的混合应用程序,如果那是最好的工作。

因此,请参阅核心数据编程指南:

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/InitializingtheCoreDataStack.html#//apple_ref/doc/uid/TP40001075-CH4-SW1

并在我现有项目的开头粘贴以下代码,生成

  

使用未解析的标识符'persistentContainer'...'managedObjectContext'

......错误。行

init(completionClosure: @escaping () -> ()) { 

...生成

  

初始化程序只能在类型

中声明
import UIKit

import CoreData
class DataController: NSObject {
  var managedObjectContext: NSManagedObjectContext
  init(completionClosure: @escaping () -> ()) {
    persistentContainer = NSPersistentContainer(name: "DataModel")
    persistentContainer.loadPersistentStores() { (description, error) in
      if let error = error {
        fatalError("Failed to load Core Data stack: \(error)")
      }
      completionClosure()
    }
  }
}

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

// followed by my existing working code:

class ViewController: UIViewController {

4 个答案:

答案 0 :(得分:32)

转到文件>新文件... iOS 下选择核心数据,然后选择数据模型 在项目创建期间选择核心数据时,您仍然需要一些xcode自动生成的代码。 要获得它,只需使用核心数据选项选中创建新项目,然后复制在 Mark: - 核心数据堆栈**中的所有代码 AppDelegate.swift 并添加

import CoreData

以上

<强> OPTIONAL

在复制 lazy var persistentContainer 的完成块后,不要忘记更改应用程序的名称。在这部分更改应用程序的名称* NSPersistentContainer(名称:&#34;应该是你的应用程序的名称&#34;)和刚刚复制的代码的managedObjectModel函数**

答案 1 :(得分:5)

如果您像我一样懒惰,这是您需要从新的Core Data项目中复制的所有代码...(为什么要让每个人都创建一个新项目?)。更改YOUR_APP_NAME_HERE

import CoreData


// MARK: - Core Data stack

func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        // Saves changes in the application's managed object context before the application terminates.
        self.saveContext()
    }

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

答案 2 :(得分:3)

我知道这已得到解答,但我认为实际问题出在Apple的文档中。如果将Objective-C代码与Swift代码进行比较,您将看到实际上未定义var managedObjectContext: NSManagedObjectContext。您应该用var persistentContainer: NSPersistentContainer替换该行。这是Objective-c接口

@interface MyDataController : NSObject
@property (strong, nonatomic, readonly) NSPersistentContainer *persistentContainer; 
- (id)initWithCompletionBlock:(CallbackBlock)callback;
@end

所以DataController.swift应该是:

class DataController: NSObject {
 // Delete this line   var managedObjectContext: NSManagedObjectContext
    var persistentContainer: NSPersistentContainer
    init(completionClosure: @escaping () -> ()) {
        persistentContainer = NSPersistentContainer(name: "DataModel")
        persistentContainer.loadPersistentStores() { (description, error) in
          if let error = error {
              fatalError("Failed to load Core Data stack: \(error)")
          }
          completionClosure()
       }
    }
}

至于你的其余代码,没有必要Apple Docs

  

在iOS 10和macOS 10.12之前,更多地涉及创建核心数据堆栈

这部分代码以旧方式向您展示。

答案 3 :(得分:0)

使用以下代码

lazy var persistantCoordinator :NSPersistentStoreCoordinator = {
    
    let poc = NSPersistentStoreCoordinator(managedObjectModel:managedObjectModel)
    
    let documentFolderUrl = FileManager.default.urls(for: .documentDirectory, in:.userDomainMask).last
    
    let path = documentFolderUrl!.appendingPathComponent("Database.sqlite")
    
    let options = [NSMigratePersistentStoresAutomaticallyOption: true,NSInferMappingModelAutomaticallyOption: true]
    do{
        try poc.addPersistentStore(ofType:NSSQLiteStoreType, configurationName: nil, at: path, options: options)
    }catch{
        print(error.localizedDescription)
    }
    
    return poc
}()


private lazy var managedObjectModel:NSManagedObjectModel = {
    
    let url = Bundle.main.url(forResource:"Database", withExtension:"momd")
    return NSManagedObjectModel(contentsOf:url!)!
}()


fileprivate lazy var managedObjectContext:NSManagedObjectContext = {
    
    let moc = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
    moc.persistentStoreCoordinator = persistantCoordinator
    
    return moc
}()