在核心数据中存储 UUID

发布于 2024-10-26 01:27:16 字数 258 浏览 4 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(3

虫児飞 2024-11-02 01:27:16

从 iOS 11 开始,您可以直接在 Core Data 编辑器中设置 UUID 属性。 UUID 将自动存储为二进制。

,您可以使用 UUID(在 Obj-C 中,NSUUID)获取 Core Data 对象。

static func fetch(with identifier: UUID) -> AudioRecord? {
    var record: AudioRecord?
    moc.performAndWait {
        let fetchRequest = AudioRecord.request
        fetchRequest.predicate = NSPredicate(format: "identifier == %@", identifier as CVarArg)
        fetchRequest.fetchLimit = 1
        record = (try? fetchRequest.execute())?.first
    }
    return record
}

Starting at iOS 11, you can set UUID attribute directly in Core Data editor. UUID will be stored as binary automatically.

enter image description here

You can then fetch your Core Data objects using UUID (in Obj-C, NSUUID).

static func fetch(with identifier: UUID) -> AudioRecord? {
    var record: AudioRecord?
    moc.performAndWait {
        let fetchRequest = AudioRecord.request
        fetchRequest.predicate = NSPredicate(format: "identifier == %@", identifier as CVarArg)
        fetchRequest.fetchLimit = 1
        record = (try? fetchRequest.execute())?.first
    }
    return record
}
书信已泛黄 2024-11-02 01:27:16

您计划存储其中多少个?将它们存储为二进制数据可节省约 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.

回首观望 2024-11-02 01:27:16

我将我的 UUID 存储为“Transformable”并且效果良好。

  1. 将其添加到您的 NSManagedObject 子类中:
    @NSManaged var uuid: NSUUID!

  2. 在核心数据模型(.xcdatamodel 文件)中,创建一个名称为 uuid 且类型为 Transformable

查询也效果很好,我编写谓词进行搜索像这样:

request.predicate = NSPredicate(format: "uuid == %@", withUuid)

I store my UUID as "Transformable" and it works well.

  1. Add this to your NSManagedObject subclass:
    @NSManaged var uuid: NSUUID!

  2. in your core data model (.xcdatamodel file), create an attribute with name uuid with Type Transformable

Querying works well too, I write my predicates to search on them like this:

request.predicate = NSPredicate(format: "uuid == %@", withUuid)

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