{"id":26959,"date":"2019-10-17T16:16:20","date_gmt":"2019-10-17T20:16:20","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=26959"},"modified":"2019-10-17T16:20:59","modified_gmt":"2019-10-17T20:20:59","slug":"core-data-derived-attributes","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2019\/10\/17\/core-data-derived-attributes\/","title":{"rendered":"Core Data Derived Attributes"},"content":{"rendered":"<p><a href=\"https:\/\/twitter.com\/numist\/status\/1184343104305762305\">Scott Perry<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/numist\/status\/1184343104305762305\">\n<p>Documentation for Core Data&rsquo;s new derived attributes feature is up!<\/p>\n<\/blockquote>\n\n<p>It&rsquo;s a really cool feature.<\/p>\n\n<p><a href=\"https:\/\/developer.apple.com\/documentation\/coredata\/nsderivedattributedescription\">NSDerivedAttributeDescription<\/a> (see also: <a href=\"https:\/\/developer.apple.com\/documentation\/coredata\/nsderivedattributedescription\/3174854-derivationexpression\">derivationExpression<\/a>):<\/p>\n<blockquote cite=\"https:\/\/developer.apple.com\/documentation\/coredata\/nsderivedattributedescription\"><p>Use derived attributes to optimize fetch performance[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>Data recomputes derived attributes when you save a context. A managed object&rsquo;s property does not reflect unsaved changes until you save the context and refresh the object.<\/p><\/blockquote>\n\n<p>That makes sense given that the derivation is implemented in the SQLite store using triggers. And you wouldn&rsquo;t want every change to cause a fetch for properties that you might not need right away.<\/p>\n\n<p>However, you have to be careful to manually refresh the objects that you care about because, since at least macOS 10.14, there&rsquo;s a bug where <code>NSFetchRequest<\/code>&rsquo;s <a href=\"https:\/\/developer.apple.com\/documentation\/coredata\/nsfetchrequest\/1506440-shouldrefreshrefetchedobjects\">shouldRefreshRefetchedObjects<\/a> option doesn&rsquo;t work (FB6161838). It fetches the right objects, but their properties may be stale. See, for example, this code:<\/p>\n\n<pre>import Foundation\nimport CoreData\n\nclass Entity: NSManagedObject {\n    @NSManaged var attribute: String\n}\n\nlet attribute = NSAttributeDescription()\nattribute.name = \"attribute\"\nattribute.attributeType = .stringAttributeType\nlet entityDescription = NSEntityDescription()\nentityDescription.name = \"Entity\"\nentityDescription.properties = [attribute]\nentityDescription.managedObjectClassName = Entity.className()\nlet model = NSManagedObjectModel()\nmodel.entities = [entityDescription]\n\nlet coordinator = NSPersistentStoreCoordinator(managedObjectModel: model)\n\/\/ Also happens with SQLite store\ntry! coordinator.addPersistentStore(ofType: NSInMemoryStoreType, configurationName: nil, at: nil, options: [:])\n\nlet writeContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)\nwriteContext.persistentStoreCoordinator = coordinator\nlet readContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)\nreadContext.persistentStoreCoordinator = coordinator\n\nlet writeEntity = Entity(entity: entityDescription, insertInto: writeContext)\nwriteContext.performAndWait {\n    writeEntity.attribute = \"Old\"\n    try! writeContext.save()\n}\n\nvar readEntity: Entity? = nil\nreadContext.performAndWait {\n    let request = NSFetchRequest&lt;Entity&gt;(entityName: entityDescription.name!)\n    readEntity = try! readContext.fetch(request).first!\n    \/\/ Initially the attribute should be Old, and that's what's printed\n    print(readEntity!.attribute)\n}\n\nwriteContext.performAndWait {\n    writeEntity.attribute = \"New\"\n    try! writeContext.save()\n}\n\nreadContext.performAndWait {\n    let request = NSFetchRequest&lt;Entity&gt;(entityName: entityDescription.name!)\n    request.shouldRefreshRefetchedObjects = true\n    _ = try! readContext.fetch(request)\n    \/\/ Now the attribute should be New, but it is still Old\n    print(readEntity!.attribute)\n\n    readContext.refresh(readEntity!, mergeChanges: false)\n    \/\/ However, manually refreshing does update it to New\n    print(readEntity!.attribute)\n}<\/pre>\n\n<p>In this example, you could instead use notifications to merge changes from one context into the other. But that wouldn&rsquo;t work with derived attributes since their changes aren&rsquo;t reported in notifications.<\/p>","protected":false},"excerpt":{"rendered":"<p>Scott Perry: Documentation for Core Data&rsquo;s new derived attributes feature is up! It&rsquo;s a really cool feature. NSDerivedAttributeDescription (see also: derivationExpression): Use derived attributes to optimize fetch performance[&#8230;] [&#8230;] Data recomputes derived attributes when you save a context. A managed object&rsquo;s property does not reflect unsaved changes until you save the context and refresh the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2019-10-17T20:16:26Z","apple_news_api_id":"e3f58fab-f5f9-4fa6-bc16-e7e6c8dbe676","apple_news_api_modified_at":"2019-10-17T20:21:04Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAABA==","apple_news_api_share_url":"https:\/\/apple.news\/A4_WPq_X5T6a8FufmyNvmdg","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":[131,109,164,31,1667,30,1609,1666,71,425,901],"class_list":["post-26959","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-bug","tag-coredata","tag-documentation","tag-ios","tag-ios-13","tag-mac","tag-macos-10-14","tag-macos-10-15","tag-programming","tag-sqlite","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26959","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=26959"}],"version-history":[{"count":6,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26959\/revisions"}],"predecessor-version":[{"id":26965,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26959\/revisions\/26965"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=26959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=26959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=26959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}