{"id":45136,"date":"2024-09-27T15:55:21","date_gmt":"2024-09-27T19:55:21","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=45136"},"modified":"2024-09-27T15:55:21","modified_gmt":"2024-09-27T19:55:21","slug":"nsmanagedobjectid-and-persistentidentifier","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2024\/09\/27\/nsmanagedobjectid-and-persistentidentifier\/","title":{"rendered":"NSManagedObjectID and PersistentIdentifier"},"content":{"rendered":"<p><a href=\"https:\/\/fatbobman.com\/en\/posts\/nsmanagedobjectid-and-persistentidentifier\/\">Fatbobman<\/a>:<\/p>\n<blockquote cite=\"https:\/\/fatbobman.com\/en\/posts\/nsmanagedobjectid-and-persistentidentifier\/\">\n<p>However, in SwiftData, there is currently no similar property or method to directly determine the [temporary] state of a <code>PersistentIdentifier<\/code>. Since SwiftData&rsquo;s <code>mainContext<\/code> defaults to the <code>autoSave<\/code> feature (developers do not need to explicitly save data), identifiers may temporarily be unusable in other contexts after creating data objects.<\/p>\n<p>[&#8230;]<\/p>\n<p>SwiftData&rsquo;s default implementation is still based on Core Data, so the format of <code>PersistentIdentifier<\/code> is very similar to that of <code>NSManagedObjectID<\/code>[&#8230;]<\/p>\n<\/blockquote>\n<p>You can see this when printing it, but crucially there is still no API to convert between the two types.<\/p>\n\n<blockquote cite=\"https:\/\/fatbobman.com\/en\/posts\/nsmanagedobjectid-and-persistentidentifier\/\">\n<p>Starting with Xcode 16, the Core Data framework has officially annotated [<code>NSManagedObjectID<\/code> as <code>Sendable<\/code>], so developers no longer need to do it manually.<\/p>\n<\/blockquote>\n<p>It&rsquo;s interesting that it took so long since <code>NSManagedObjectID<\/code> has always been threadsafe outside of Swift Concurrency.<\/p>\n<blockquote cite=\"https:\/\/fatbobman.com\/en\/posts\/nsmanagedobjectid-and-persistentidentifier\/\">\n<p>Although an <code>NSManagedObjectID<\/code> instance contains sufficient information to indicate the data post-persistence, it cannot retrieve the corresponding data when used with another <code>NSPersistentStoreCoordinator<\/code> instance, even if the same database file is used. In other words, an <code>NSManagedObjectID<\/code> instance cannot be used across coordinators.<\/p>\n<p>This is because the <code>NSManagedObjectID<\/code> instance also includes private properties of the corresponding <code>NSPersistentStore<\/code> instance.<\/p>\n<\/blockquote>\n<p>I kind of look at it backwards from this. The <code>NSManagedObjectID<\/code> <em>doesn&rsquo;t<\/em> contain very much information&mdash;it&rsquo;s often squeezed into a tagged pointer. How is this possible when it logically contains a reference to the entity description and the persistent store, in addition to the primary key? I assume that it&rsquo;s storing abbreviations for these, which can only be interpreted with respect to a coordinator.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/07\/26\/swiftdata-and-core-data-at-wwdc24\/\">SwiftData and Core Data at WWDC24<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/06\/04\/swiftdata-issues-in-macos-14-and-ios-17\/\">SwiftData Issues in macOS 14 and iOS 17<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2023\/06\/12\/swiftdata\/\">SwiftData<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Fatbobman: However, in SwiftData, there is currently no similar property or method to directly determine the [temporary] state of a PersistentIdentifier. Since SwiftData&rsquo;s mainContext defaults to the autoSave feature (developers do not need to explicitly save data), identifiers may temporarily be unusable in other contexts after creating data objects. [&#8230;] SwiftData&rsquo;s default implementation is still [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2024-09-27T19:55:23Z","apple_news_api_id":"fa02990b-2f28-48d9-bcdd-a0f1ed3de4ce","apple_news_api_modified_at":"2024-09-27T19:55:23Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/A-gKZCy8oSNm83aDx7T3kzg","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[109,31,2586,30,2598,71,2200,2404],"class_list":["post-45136","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-coredata","tag-ios","tag-ios-18","tag-mac","tag-macos-15-sequoia","tag-programming","tag-swift-concurrency","tag-swiftdata"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/45136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=45136"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/45136\/revisions"}],"predecessor-version":[{"id":45137,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/45136\/revisions\/45137"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=45136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=45136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=45136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}