简体   繁体   English

在应用程序退出时清除的核心数据属性(或不保存到永久性存储)

[英]Core Data properties that are cleared on app exit (or not save to persistent store)

I want to store some additional attributes in my managed objects that are not saved to the persistent store together with other (persistent) properties, ie stored only in memory, so that after the app is restarted these non-persistent attributes are reset back to default values. 我想在我的托管对象中存储一些未与其他(持久性)属性一起保存到持久性存储中的其他属性,即仅存储在内存中,以便在应用程序重新启动后,将这些非持久性属性重置为默认值价值观。

I've looked into using transient properties but they are lost when the object is fetched again from another screen, so not suitable for my case. 我已经研究过使用瞬态属性,但是当从另一个屏幕再次获取对象时,它们会丢失,因此不适合我的情况。

Does Core Data have something like this? 核心数据有这样的东西吗?

I have had good experience with storing extra values outside of core-data. 我在存储核心数据之外的额外值方面拥有丰富的经验。 I used a dictionary where the key is the id of the core-data property and the value is another mutable dictionary. 我使用了一个字典,其中的键是core-data属性的ID,而值是另一个可变字典。 This allows quick lookup for any of these extra properties. 这样可以快速查找这些额外属性中的任何一个。 I used it in a messaging app to track currently online status. 我在消息传递应用程序中使用它来跟踪当前的在线状态。

The main downside is that all searching and sorting has to be done in-memory and not on the database level, but it was not a big problem. 主要缺点是所有搜索和排序都必须在内存中完成,而不是在数据库级别进行,但这不是一个大问题。

If you're using subclasses of NSManagedObject (which is usually true) then you can just add extra properties to those subclasses. 如果您使用的是NSManagedObjectNSManagedObject (通常是这样),则可以向这些子类添加额外的属性。 There's nothing special about these properties, because not all of the properties need to be managed. 这些属性没有什么特别的,因为不是所有的属性都需要管理。 Create them as you would on any other class. 就像在其他任何班上一样创建它们。

You'll want to tell Xcode to use "Category/Extension" code generation for the entity instead of "Class Definition", so that you can edit the class file. 您将要告诉Xcode为实体使用“类别/扩展”代码生成而不是“类定义”,以便您可以编辑类文件。 But once you've done that, just add whatever properties you need. 但是一旦完成,只需添加所需的任何属性即可。 Core Data won't know or care about them. 核心数据不会知道或不在乎它们。

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

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