ios 数据持久化_iOS持久性和核心数据

ios 数据持久化

All the variables and constants that you allocate in Swift are stored in memory, so they get lost when users quit the app.

您在Swift中分配的所有变量和常量都存储在内存中,因此当用户退出应用程序时它们会丢失。

Persistence is saving data to a place where it can be re-accessed and retrieved upon restart of the device or app. So, that is longer than a life cycle the running app and keeps it available to the user.

持久性将数据保存到可以在重新启动设备或应用程序后重新访问和检索的位置。 因此,这比正在运行的应用程序的生命周期更长,并使用户可以使用它。

UserDefaults is by far the easiest way to persist your data to your device. It’s like a dictionary that automatically saves its contents. With all that;

到目前为止, UserDefaults是将数据持久保存到设备的最简单方法。 就像字典一样,它会自动保存其内容。 有了这些

  • It saves data into p.list

    它将数据保存到p.list
  • We need to keep the size below 1 MB. Because its reads and writes all in one go.

    我们需要将大小保持在1 MB以下。 因为它可以一次性读写。

But if the data is getting bigger and complex?

但是,如果数据变得越来越大和复杂?

Every app has a data layer. In a cooking app data might include recipes, ingredients. Within this data there are often relationships. For example, a recipe may call for many ingredients. While an ingredient should be free to appear in multiple recipes. The recipe app’s data includes structure that is clearly defined object types, and relationships between instances of the types.

每个应用程序都有一个数据层。 在烹饪应用程序中,数据可能包括食谱,配料。 在这些数据中通常存在关系。 例如,食谱可能需要许多成分。 虽然一种成分应该可以自由出现在多个配方中。 食谱应用程序的数据包括明确定义的对象类型的结构以及类型实例之间的关系

Image for post

We use CoreData to manage data layer.

我们使用CoreData来管理数据层。

Persistence is just one aspect of data layer management.

持久性只是数据层管理的一个方面。

CoreData (data layer management framework) saves data into something called a persistent store. The store is where the data lives. There are 3 different types of stores CoreData supports on iOS;

CoreData( 数据层管理框架 )将数据保存到称为持久性存储的内容中。 存储是数据所在的地方。 在iOS上,CoreData支持3种不同类型的商店;

  • SQLite Store (default)

    SQLite Store(默认)
  • Binary Store

    二进制存储
  • In-memory Store

    内存存储

SQLite is almost always the right choice of your persistent store in a SQL relational database.

对于SQL关系数据库中的持久存储, SQLite几乎总是正确的选择。

Binary store can be appropriate when you always need the database to be read and written in its entirety.

当您始终需要整体读取和写入数据库时​​, 二进制 存储可能是合适的。

In-memory store can be appropriate when you have a small data model can fit in memory all at once and that does not need to be saved to disk. (like cache)

当您的小型数据模型可以一次全部放入内存且不需要保存到磁盘时, 内存中存储可能是合适的。 (如缓存)

CoreData abstracts the persistent store’s details. That means you won’t usually interact with the store directly. That provides a common interface for saving and fetching data no matter what kind of store sits below. Whatever type of store you choose you’ll always use the same (CoreData). And, you won’t need to learn a database-specific language to manage your data.

CoreData提取持久性存储的详细信息。 这意味着您通常不会直接与商店互动。 这提供了用于保存和获取数据的通用接口,无论下面是哪种类型的商店。 无论您选择哪种存储类型,都将始终使用相同的(CoreData)。 而且, 您将无需学习特定于数据库的语言来管理数据。

还有其他保存数据的方法吗? (Are There Other Ways to Save Data?)

Yes. Although Core Data is a great solution for persisting data that has structure and relationships, other solutions are better suited for other types of data. For settings or small pieces of data, UserDefaults is most appropriate. If you want to store data directly on the file system, you can use the Foundation framework to serialize and write data to files. If you are familiar with SQLite, then you could use it directly instead of through Core Data. There are also ways of caching transient, downloaded data such as the result of a network request.

是。 尽管Core Data是用于持久存储具有结构和关系的数据的绝佳解决方案,但其他解决方案更适合于其他类型的数据。 对于设置或少量数据, UserDefaults最合适。 如果要直接在文件系统上存储数据,则可以使用Foundation框架进行序列化并将数据写入文件。 如果您熟悉SQLite,则可以直接使用它而不是通过Core Data。 还有一些方法可以缓存临时下载的数据,例如网络请求的结果。

Third party databases and persistence frameworks like Realm and Firebase provide useful features like syncing local and remote data, and cross-platform support for iOS and Android. But, while powerful, these frameworks often lack UIKit integration, and they may not implement the full extent of Core Data features. It’s up to you to consider the tradeoffs.

第三方数据库和持久性框架(如RealmFirebase)提供了有用的功能,例如同步本地和远程数据,以及对iOS和Android的跨平台支持。 但是,尽管这些框架功能强大,但它们通常缺少UIKit集成,因此它们可能无法实现Core Data功能的全部范围。 由您决定权衡取舍。

翻译自: https://medium.com/macoclock/ios-persistence-and-core-data-c3c23e7152f3

ios 数据持久化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值