fatbobman / blogComments

1 stars 0 forks source link

NSManagedObjectID 与 PersistentIdentifier #250

Open fatbobman opened 1 month ago

fatbobman commented 1 month ago

NSManagedObjectID and PersistentIdentifier: Mastering Data Identifiers in Core Data and SwiftData

Core Data and SwiftData are powerful data management frameworks designed by Apple for developers, capable of efficiently handling complex object relationships, hence known as object graph management frameworks. In these two frameworks, NSManagedObjectID and PersistentIdentifier serve similar functions and are both extremely important. This article will delve into their features, usage methods, and important considerations.

NSManagedObjectID 与 PersistentIdentifier:掌握 Core Data 与 SwiftData 中的数据标识符

Core Data 和 SwiftData 是苹果为开发者设计的强大数据管理框架,能够高效处理复杂的对象关系,因而被称为对象图管理框架。在这两个框架中,NSManagedObjectIDPersistentIdentifier 功能相似,且都极为重要。本文将深入探讨它们的功能、使用方法及注意事项。

linqingmo commented 1 month ago

Xcode16 SwiftData的PersistentIdentifier有了变化。SwiftData.PersistentIdentifier.ID(url: x-swiftdata://Work/51DA9905-C4A5-4BB7-A213-56A308597619),出现了x-swiftdata。复现方式,把Id传给WindowGroup,然后重新运行,程序会闪退,log提示有x-swiftdata。

WindowGroup(id: "Main", for: PersistentIdentifier.self) { id in
       if let id = id.wrappedValue, let work = context.model(for: id) as? Work {
             MainView(work: work)
       }
}

SwiftData/BackingData.swift:866: Fatal error: This model instance was invalidated because its backing data could no longer be found the store. PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(url: x-swiftdata://Work/51DA9905-C4A5-4BB7-A213-56A308597619), implementation: SwiftData.PersistentIdentifierImplementation)

看来苹果是想把新的SwiftData更多地从CoreData抽离出来,但是显然,第一个更新出现了bug。

fatbobman commented 1 month ago

@linqingmo x-swiftdata://Work/51DA9905-C4A5-4BB7-A213-56A308597619 这个格式是未持久化的 id,另外,context.mode 的方式也是最不安全的一种提取方式。 你可以先持久化后再试一下。iOS 18 中 ,autoSave 的延迟很高,最好在代码中手动 save

linqingmo commented 1 month ago

@fatbobman @linqingmo x-swiftdata://Work/51DA9905-C4A5-4BB7-A213-56A308597619 这个格式是未持久化的 id,另外,context.mode 的方式也是最不安全的一种提取方式。 你可以先持久化后再试一下。iOS 18 中 ,autoSave 的延迟很高,最好在代码中手动 save

感谢解答