将 MKOverlay 对象保存在 Core Data 中

发布于 2024-12-14 02:55:18 字数 240 浏览 0 评论 0原文

我有一个很大的 MKOverlay,我想将其保存在 Core Data 中,这样我以后就不必创建它。由于这不是您可以在 Core Data 中选择的类型之一,我该如何保存它?

我需要先以某种方式对其进行编码吗?

那么使用时需要解码吗?

创建新属性时,我应在核心数据中选择什么类型的对象?

谢谢你们。

I have a large MKOverlay that I would like to be saved in Core Data so that I don't have to create it later. Since this isn't one of the types that you can choose in Core Data, how do I go about saving it?

Do I need to somehow encode it first?

Do I then need to decode it when using?

What kind of object do I select in core data when creating a new property?

Thanks guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

星星的轨迹 2024-12-21 02:55:18

如果您不需要查询不同的覆盖层,并且您没有在项目中的其他地方使用核心数据,那么您最好将覆盖层作为编码的 NSArray 缓存在磁盘上。

但是,如果您已经在使用 Core Data 或者正在缓存多个覆盖,那么您可以在 NSData 类型的字段中对覆盖进行编码/解码。向实体添加其他字段,以便您可以查询要查找的特定叠加层。

在 iOS 5 中,您可以通过选择“允许外部存储”选项来启用外部文件中 NSData 字段的可选存储。 Core Data 将应用基于大小的启发式方法来确定 blob 或外部文件是否会带来更好的性能。

MKOverlay 符合 NSCoding,因此您可以使用 NSKeyedArchiver 的编码方法对整个 MKOverlay 对象数组进行编码和解码,并将结果存储在实体中的二进制字段中。您可能需要 + (NSData *)archivedDataWithRootObject:(id)rootObject on NSKeyedArchiver 和 + (id)unarchiveObjectWithData:(NSData *)data on NSKeyedUnarchiver

有关创建密钥存档的详细信息,请参阅存档和序列化编程指南中的存档部分在:http://developer.apple.com/library/ ios/#documentation/Cocoa/Conceptual/Archiving/Articles/archives.html

您可以为实体的二进制字段编写自定义访问器,用于对覆盖数组进行编码和解码 你。另一种选择是创建一个封装编码和解码操作的值转换器。最终结果将是一个覆盖数组属性,您可以通过entity.overlays设置和读取该属性。

If you do not need to query for different overlays and you're not using core data elsewhere in your project, then you're probably better off caching the overlay on disk as an encoded NSArray.

However, if you're already using Core Data or you're caching multiple overlays then you can encode/decode the overlay in a field of type NSData. Add additional fields to the entity so you can query for the specific overlay you're looking for.

In iOS 5, you can enable optional storage of NSData fields in an external file by selecting the "Allows External Storage" option. Core Data will apply a size-based heuristic to determine if a blob or external file will result in better performance.

MKOverlay conforms to NSCoding, so you can encode and decode an entire array of MKOverlay objects using an encode method of NSKeyedArchiver and store the result in a binary field in your entity. You'll likely want + (NSData *)archivedDataWithRootObject:(id)rootObject on NSKeyedArchiver and + (id)unarchiveObjectWithData:(NSData *)data on NSKeyedUnarchiver

See the Archives section in the Archives and Serializations Programming guide for details of creating a keyed archive at: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Archiving/Articles/archives.html

You can write a custom accessor for the entity's binary field that encodes and decodes the overlay array for you. Another option is to create a value transformer that encapsulates the encoding and decoding operations. The end result would be an overlays array property that you can set and read via entity.overlays.

绿光 2024-12-21 02:55:18

我相信你可以使用苹果的 NSCoding 库,用于将对象与序列化状态相互转换。但是,Core Data 可能支持保存对象,但 NSCoding 允许您在任何地方保存实现它的任何类,包括发送到服务器的字符串、写入磁盘的文件或如果你和我一样是个糟糕的程序员,一个 NSUserDefaults 条目。

编辑-您可能必须通过添加读取和写入方法来将 NSCoding 实现到您自己的基于 MKOverlay 的类中,我不确定。

I believe you can use Apple's NSCoding libraries to convert the object to and from a serialized state. However, Core Data may support saving objects, but NSCoding lets you save any class that implements it anywhere, including a string sent to a server, a file written to disk, or if you're as bad a programmer as me, an NSUserDefaults entry.

edit- You may have to implement NSCoding into your own class based on MKOverlay by adding read and write methods, I'm uncertain.

无声静候 2024-12-21 02:55:18

为什么不保存属性(大小、颜色、坐标等都可以用 NSNumbers 描述,并且这些属性可以本地存储在 Core Data 中)并在需要时重新创建 MKOverlay。老实说,我认为这是一种更有效的方法。我不确定创建一个对象会产生多大的影响,所以如果我错了,请证明我是错的。

Why not instead save the properties (size, color, coordinates, etc can all be described with NSNumbers and those can be stored in Core Data natively) and recreate the MKOverlay when needed. I think that's a much more efficient approach to be honest. I'm not sure how much of an impact creating an object has, so prove me wrong if I'm wrong.

把人绕傻吧 2024-12-21 02:55:18

您需要获取构成覆盖的大型数据集,并将这些单独的数据节点转换为 NSManagedObjects 以存储在 CoreData 中。

我的意思是,您可能可以将整个内容 NSCoder 放入一个巨大的数据块中,但此时,您也可以将其写入一个平面文件(坦率地说,如果您只想读/写它,这可能会更好)而不改变它)。

除非您要对数据集进行合法查询或零碎更改,否则不要使用核心数据。

You need to take the large dataset that composes the overlay and turn those individual data nodes into NSManagedObjects to be stored in CoreData.

I mean, you probably COULD just NSCoder the entire thing into one giant datablob, but at that point, you might as well just write the thing to a flat file (which frankly might be better if all you want to do is read/write it without changing it).

Don't use Core Data unless you're going to be doing legit querying or piecemeal changes to the dataset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文