简体   繁体   English

了解iOS Swift中的CoreData

[英]Understanding CoreData in iOS Swift

I am building an app at the moment and am trying to get my head around the concept of CoreData. 目前,我正在构建一个应用程序,并试图让我了解CoreData的概念。 I have created a data model for the app based on the advice from this link... 我已根据此链接的建议为该应用创建了数据模型...

Apple "Define Your Data Model" Link 苹果“定义您的数据模型”链接

Currently, I am just using some test data locally for development purposes. 目前,我只是出于开发目的在本地使用一些测试数据。 Here is the sample object class I have created... 这是我创建的示例对象类。

import UIKit

func ==(lhs: Recipe, rhs: Recipe) -> Bool {
    return lhs.hashValue == rhs.hashValue
}

class Recipe: Hashable {

dynamic var ID : Int = 0
dynamic var recipeName: String = ""
dynamic var recipeDescription:String = ""
dynamic var servings: Int = 0
dynamic var cookTime: Double = 0.0
dynamic var image: String? = ""

var hashValue : Int {
    get {
        return "\(self.ID)".hashValue
    }
}
init?(id: Int, name: String, description: String, servings: Int, cooktime: Double, image: String) {
    self.ID = id
    self.recipeName = name
    self.recipeDescription = description
    self.servings = servings
    self.cookTime = cooktime
    self.image = image

    if id < 0 || name.isEmpty || description.isEmpty || cookTime < 0 || servings < 0 {
        return nil
    }
}
}

I have then initialized some of those objects locally to be used during development and they work fine. 然后,我已经在本地初始化了其中一些对象,以便在开发过程中使用它们,并且它们工作正常。

My question is... 我的问题是

When it comes time to use storage like CoreData or RealmSwift, will this type of data model be irrelevant? 当需要使用诸如CoreData或RealmSwift之类的存储时,这种类型的数据模型会不相关吗? Will I make an entirely new data model (in Realm for example)? 我会建立一个全新的数据模型(例如在Realm中)吗? or will this type of data model play nicely with one of the DB models? 还是这种数据模型可以与其中一种数据库模型很好地配合使用?

Most importantly, will building my data model like this now and incorporating CoreData or RealmSwift after be the cause of a lot of code refactoring? 最重要的是,现在像这样构建我的数据模型并在大量代码重构之后合并CoreData或RealmSwift吗? If this is the case then would it be advisable to drop the frontend development and build the entire data model before moving forward? 如果是这种情况,那么在继续前进之前,放弃前端开发并构建整个数据模型是否明智?

Core Data and Realm go two very different approaches for defining your schema. 核心数据和领域使用两种截然不同的方法来定义模式。

For Core Data, you need to design your model in Xcode's Data Model editor, which creates a bundle of xcdatamodel files for each version. 对于Core Data,您需要在Xcode的数据模型编辑器中设计模型,该编辑器会为每个版本创建一捆xcdatamodel文件。 You can then either use Xcode built-in capabilities or some third party tooling as mogenerator to generate your NSManagedObject entity classes. 然后,您可以使用Xcode内置功能或某些第三方工具作为生成来生成NSManagedObject实体类。 Core Data can automatically infer a mapping between different versions of your schema with lightweight migrations , but this works only within certain limitations. Core Data可以通过轻量级迁移自动推断模式的不同版本之间的映射,但这仅在某些限制下有效。 If your migration case should be more complex, you can define custom mapping models. 如果您的迁移情况应该更复杂,则可以定义自定义映射模型。

With Realm in general and RealmSwift specifically here, you define your schema (and migrations) completely in code. 通过一般的Realm和此处的RealmSwift,您可以完全在代码中定义架构(和迁移)。 Your classes have to inherit from Object and you have to define your properties conforming to some rules , so that they are recognized correctly and can be automatically persisted. 您的类必须从Object继承,并且必须定义符合某些规则的属性,以便正确识别它们并可以将它们自动保留。

But the decision for one persistency framework over the other will have application-wide effects. 但是,一个持久性框架相对于另一个持久性框架的决定将对整个应用程序产生影响。 For example Core Data and Realm have different requirements regarding multi-threading. 例如,Core Data和Realm对多线程有不同的要求。 While you can theoretically avoid that if you encapsulate your persistency layer completely and share only proxy objects with your frontend, you'll loose at the same time the advantage of component support ( NSFetchedResultsController / RBQFetchedResultsController ) and further conveniences the persistency framework may offer. 虽然理论上可以规避,如果你完全封装的持久层,并与您分享前端仅代理对象,你会松动的同时的组件支持(优势NSFetchedResultsController / RBQFetchedResultsController ),并进一步便利持久性框架可以提供。 (eg change notifications, auto-updates, …) (例如,更改通知,自动更新等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM