在核心数据中存储 UUID
在 Core Data 中存储 UUID(用于全局多系统对象标识)的最佳方法是什么?考虑到存储大小和索引功能。
理想情况下,它将存储为二进制数据(128 位),但是这样做会立即出现问题吗?以这种方式存储它而不是作为 NSString 在大小方面会更有效,但我只是想检查将其存储为二进制数据是否没有性能问题。它仍然会被正确索引为二进制数据吗?在可变宽度字段中存储有效的固定宽度二进制数据是否有任何缺点?
我对 SQLite 及其存储/索引机制不太熟悉,因此想寻求一些建议!
What is the best method for storing UUIDs (for global multi-system object identification) in Core Data? Taking into account storage size and indexing capabilities.
Ideally it would be stored as binary data (in 128-bits), but are there any immediate problems with doing this? It would be more efficient size-wise storing it this way rather than as an NSString, but I just want to check that there's no performance issues with storing this as binary data. Will it still be properly indexed as binary data? Are there any disadvantages storing what is effectively fixed-width binary data in a variable width field?
I'm not overly familiar with SQLite and it's storage/indexing mechanisms so wanted to reach out for some advice!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 iOS 11 开始,您可以直接在 Core Data 编辑器中设置 UUID 属性。 UUID 将自动存储为二进制。
,您可以使用
UUID
(在 Obj-C 中,NSUUID
)获取 Core Data 对象。Starting at iOS 11, you can set UUID attribute directly in Core Data editor. UUID will be stored as binary automatically.
You can then fetch your Core Data objects using
UUID
(in Obj-C,NSUUID
).您计划存储其中多少个?将它们存储为二进制数据可节省约 50%,即二进制数据大约 20 个字节,而字符串大约 40 个字节。所以你说的是每 1000 个 UUID 可以节省 20K,这还不足以让我担心。但是,如果您确实想将它们保存为二进制数据,可以通过将它们存储为 NSData 对象来实现。
How many of these are you planning on storing? Storing them as binary data saves about 50% -- roughly 20 bytes as binary data vs. roughly 40 bytes as a string. So you're talking about saving a whole 20K per thousand UUID's, which isn't so much that I'd worry about it either way. However, if you really want to save them as binary data, you can do that by storing them as NSData objects.
我将我的 UUID 存储为“Transformable”并且效果良好。
将其添加到您的
NSManagedObject
子类中:@NSManaged var uuid: NSUUID!
在核心数据模型(.xcdatamodel 文件)中,创建一个名称为
uuid
且类型为Transformable
查询也效果很好,我编写谓词进行搜索像这样:
request.predicate = NSPredicate(format: "uuid == %@", withUuid)
I store my UUID as "Transformable" and it works well.
Add this to your
NSManagedObject
subclass:@NSManaged var uuid: NSUUID!
in your core data model (.xcdatamodel file), create an attribute with name
uuid
with TypeTransformable
Querying works well too, I write my predicates to search on them like this:
request.predicate = NSPredicate(format: "uuid == %@", withUuid)