我真的很想避免使用它NSManagedObjectID作为将我的模型结构连接到其 CoreData 对象的方式。我的意思是这样的:
假设我Book在 CoreData 中有一个实体,然后我有一个像这样的模型结构,代表我的模型层:
struct BookModel {
let name: String
...
let objectID: NSManagedObjectID // I need this to refer back to the entry in the database
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢这种方法。它使得使用结构变得乏味,例如,测试很烦人,因为我总是必须生成虚拟 objectId 或使其成为BookModel.objectID可选。
我想要的是实体内部id的类型属性。这将很容易连接到结构,并且还允许结构在没有数据库的情况下正确存在:UUIDBook
struct BookModel {
let name: String
...
let id: UUID
...
func object() -> Book {
// Retrieve managed object using a fetch request with a predicate.
}
}
Run Code Online (Sandbox Code Playgroud)
我注意到您实际上可以UUID在实体中拥有属性。然而,性能差异似乎是巨大的。我创建了一个尝试获取单个对象 10000 次的示例。
首先,我使用context …
尝试创建本地通知失败,并显示以下错误:
Adding notification request failed with error: Error
Domain=NSCocoaErrorDomain Code=4097 "connection to service named
com.apple.usernotifications.usernotificationservice" UserInfo=
{NSDebugDescription=connection to service named
com.apple.usernotifications.usernotificationservice}
Run Code Online (Sandbox Code Playgroud)
这意味着什么?