Core Data Batch Updating Properties

Originator:brentsimmons
Number:rdar://15183235 Date Originated:10/8/2013
Status:Open Resolved:
Product:iOS SDK Product Version:iOS 7
Classification:Feature (New) Reproducible:
 
Summary:

Picture an RSS reader, podcast client, or Twitter app that needs to mark large numbers of objects as read all at once. (The Google Reader API, for instance, used to return a set of 10,000 article IDs to mark as read.)

Right now it's necessary to fetch each object, flip the read bit to YES, and save.

Better would be to not have to fetch each object.

I'd like to write code something like this:

[managedObjectContext updateProperties:@{@"read" : @YES} forObjectsMatchingPredicate:predicate entityName:@"Articles"];

Comments

Fetches are inexpensive, faulting is not - but faulting can be batched

Fetching the objects should be inexpensive. It is faulting them that is the expensive read operation (in your case, faulting isRead). That can be done in batches efficiently: https://developer.apple.com/library/mac/documentation/cocoa/conceptual/coredata/Articles/cdPerformance.html#//apple_ref/doc/uid/TP40003468-SW6 Then once you have changed the value, you can save in whatever batch size you need. When the save occurs, if you are using thread confinement this will trigger a notification that should be merged into other contexts - that is typically where developers see performance problems, handing the incoming changes to the main thread. If you saved your changed objects in batches of 50, the impact should be minimal. Or you could avoid the problem altogether by using parent-child contexts with queue confinement, which do not require the processing of merge notifications and handle these use cases much more efficiently.


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!