如何将核心数据添加到现有的 Xcode 9 Swift 4 iOS 11 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48723979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:33:03  来源:igfitidea点击:

How to add Core Data to existing Xcode 9 Swift 4 iOS 11 project?

iosxcodecore-data

提问by ehounder

An update is requested, since this question has of course been answered for previous versions, the latest search result dated 12/16 generates irrelevant compatibility with previous iOS 9 and 10 projects.

请求更新,因为这个问题当然已经在以前的版本中得到了回答,日期为 12/16 的最新搜索结果与之前的 iOS 9 和 10 项目产生了不相关的兼容性。

The documentation of course says to select the Use Core Data checkbox when starting a new project, which I did not select, but now think iCloud + Core Data needs to be added to take my app to its next phase -> wherein something like NSFileCoordinator and NSFilePresenter is needed, since in my app UI users are presented with a number of TOPICS, each having three OPTIONS, regarding which users are to choose one option. For each topic the UI then displays the TOTAL NUMBER of users who have chosen each option and the PERCENTAGE of the total for each option.

文档当然说在开始一个新项目时选择 Use Core Data 复选框,我没有选择,但现在认为需要添加 iCloud + Core Data 以将我的应用程序带到下一个阶段 -> 其中像 NSFileCoordinator 和NSFilePresenter 是必需的,因为在我的应用程序 UI 中,用户会看到许多主题,每个主题都有三个选项,关于哪些用户要选择一个选项。对于每个主题,用户界面然后显示选择每个选项的用户总数和每个选项总数的百分比。

Right now, the number of choices for each option and the percentage of the total are of course just calculated in my native app -> but actually need to be CALCULATED in something central like the cloud or most likely on a website…but then the website raises the simultaneous read/write problems that NSFileCoordinator and NSFilePresenter have already solved.

现在,每个选项的选择数量和总数的百分比当然只是在我的本机应用程序中计算出来的 -> 但实际上需要在诸如云或最有可能在网站上的中心进行计算......但然后是网站引发了 NSFileCoordinator 和 NSFilePresenter 已经解决的同步读/写问题。

So if the iCloud + Core Data system can interject basic arithmetic calculations on the existing Ubiquitous Container numerical value totals - in the cloud upon receiving write numerical value commands from individual users - before sending out the new Ubiquitous Container numerical total and percent values - then I'd much appreciate advise on how fix the errors generated below in trying Create and Initialize the Core Data Stack. Otherwise guess I'll have to scrape Xcode and go to a hybrid app like PhoneGap if that's the best one for the job.

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

Hence, referring to the Core Data Programming Guide:

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

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

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

and pasting in the following code in the beginning of my existing project, generates

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

Use of unresolved identifier ‘persistentContainer'… ‘managedObjectContext'

使用未解析的标识符“persistentContainer”…“managedObjectContext”

... errors. And the line

...错误。和线

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

... generates

... 产生

Initializers may only be declared within a type

初始化器只能在一个类型中声明

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 {

回答by Jay N.

go to File > new file...select core Dataunder iOSand select Data Modelyou'll still need some code which xcode auto generates whenever you select core data during project creation. to get it, just create new project with core data option checkedand copy all the code written under ** //Mark: - Core Data Stack** comment in AppDelegate.swiftand add

转到File > new file...iOS下选择core Data并选择Data Model您仍然需要一些代码,每当您在项目创建期间选择核心数据时,xcode 都会自动生成这些代码。要获得它,只需在选中核心数据选项的情况下创建新项目并复制在 ** //Mark: - Core Data Stack** 注释下编写的所有代码AppDelegate.swift并添加

import CoreData

above

以上

OPTIONAL

可选的

And don't forget to change the name of the app after copying the completion block for lazy var persistentContainer. Change the name of your app on this part *NSPersistentContainer(name: "SHOULD-BE-THE-NAME-OF-YOUR-APP") And managedObjectModel function of the code you just copied**

并且不要忘记在复制惰性 var persistentContainer的完成块后更改应用程序的名称。在这部分更改您的应用程序的名称 *NSPersistentContainer(name: "SHOULD-BE-THE-NAME-OF-YOUR-APP") 和您刚刚复制的代码的 managedObjectModel 函数**

回答by iOSDevSF

If you're lazy like me, here's all the code you need to copy from the new Core Data project... (why make everyone create a new project?). Change YOUR_APP_NAME_HERE

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

At the top of your AppDelegate.swift file:

在 AppDelegate.swift 文件的顶部:

import CoreData

At the bottom of AppDelegate.swift file, before the ending curly bracket:

在 AppDelegate.swift 文件的底部,在结束大括号之前:

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

回答by hidden-username

I know this is answered, but I believe the actual problem is with Apple's Documentation. If you compare the Objective-C code to the Swift code, you will see that var managedObjectContext: NSManagedObjectContextis not actually defined. You should replace that line with var persistentContainer: NSPersistentContainer. This is the Objective-c interface

我知道这已得到解答,但我相信实际问题出在 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

So DataController.swiftshould be:

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

As for the rest of your code, it's not necessary Apple Docs.

至于您的其余代码,则不需要Apple Docs

Prior to iOS 10 and macOS 10.12, the creation of the Core Data stack was more involved

在 iOS 10 和 macOS 10.12 之前,Core Data 堆栈的创建涉及更多

That section of code is showing you the old way.

那部分代码向您展示了旧方法。