{"id":9490,"date":"2014-09-08T11:31:05","date_gmt":"2014-09-08T15:31:05","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=9490"},"modified":"2014-09-08T12:24:34","modified_gmt":"2014-09-08T16:24:34","slug":"core-data-batch-updates","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/09\/08\/core-data-batch-updates\/","title":{"rendered":"Core Data Batch Updates"},"content":{"rendered":"<p><a href=\"http:\/\/weblog.invasivecode.com\/post\/96947153405\/core-data-batch-updates-in-ios-8-and-swift-core\">Geppy Parziale<\/a>:<\/p>\n<blockquote cite=\"http:\/\/weblog.invasivecode.com\/post\/96947153405\/core-data-batch-updates-in-ios-8-and-swift-core\">\n<p>First of all, Apple introduced a new method <code>executeRequest:error:<\/code> in the <code>NSManagedObjectContext<\/code> class. It is very similar to the <code>executeFetchRequest:error:<\/code> method that you can use to perform a fetch request to populate the Managed Object Context. Its first argument is a <code>NSBatchUpdateRequest<\/code>. This is a new class, subclass of the <code>NSPersistentStoreRequest<\/code> recently introduced in iOS 7, and provides very similar functionalities to its sibling <code>NSFetchRequest<\/code>. The batch request is composed of an entity (the entity containing the property or properties you want to update) and a predicate to define a subset of data you want to update. Eventually, you can also define a dictionary containing the properties you want to update and their new values.<\/p>\n<p>Once created, the <code>NSBatchUpdateRequest<\/code> is passed to the <code>executeRequest:error:<\/code> method. After its execution, this method returns an <code>NSBatchUpdateResult<\/code> object (subclass of <code>NSPersistentStoreResult<\/code>), the <code>result<\/code> property of which contains the batch updates result value(s). You can define the type of results you want from the <code>executeRequest:error:<\/code>, when you define the <code>NSBatchUpdateRequest<\/code>. You choose among three types of results:<\/p>\n<p>[&#8230;]<\/p>\n<p>The legacy Fetch-Update-Save takes 7.2s to run on the iPhone 5s, while the new batch updates run in only <em>0.81&nbsp;s<\/em>.<\/p>\n<p>Impressive is also the memory usage. As expected, since the batch updates run directly on the Persistent Store, the memory usage is incredibly lower than the old approach.<\/p>\n<\/blockquote>\n<p>I&rsquo;ve been <a href=\"http:\/\/mjtsai.com\/blog\/2010\/02\/26\/on-switching-away-from-core-data\/\">wanting<\/a> something like this for a long time. (Note that it doesn&rsquo;t handle deletion.)<\/p>","protected":false},"excerpt":{"rendered":"<p>Geppy Parziale: First of all, Apple introduced a new method executeRequest:error: in the NSManagedObjectContext class. It is very similar to the executeFetchRequest:error: method that you can use to perform a fetch request to populate the Managed Object Context. Its first argument is a NSBatchUpdateRequest. This is a new class, subclass of the NSPersistentStoreRequest recently introduced [&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":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","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":[69,109,31,904,30,903,138,71],"class_list":["post-9490","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-coredata","tag-ios","tag-ios-8","tag-mac","tag-mac-os-x-10-10-yosemite","tag-optimization","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9490","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=9490"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9490\/revisions"}],"predecessor-version":[{"id":9494,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9490\/revisions\/9494"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=9490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=9490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=9490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}