michaelarmstrong / SuperRecord

A small set of utilities to make working with CoreData and Swift a bit easier.
MIT License
366 stars 27 forks source link

Add batchUpdate #29

Closed PGLongo closed 9 years ago

PGLongo commented 9 years ago

We could add batchUpdate with few lines of code

var request = NSBatchUpdateRequest(entityName: "Pokemon");
request.propertiesToUpdate = ["level" : 100]
request.resultType = NSBatchUpdateRequestResultType.UpdatedObjectsCountResultType
let result =  self.managedObjectContext.executeRequest(request, error: nil) as NSBatchUpdateResult;
 result.result // number of row update

Made a simple test creating 100000 Pokemon and the results are:

Operation Time
Insert 37.096 seconds
Update 0.363 seconds
Delete 0.135

I have also tried update the record without batchUpdate and as expected it's necessary to save the context more than one time (for the test every 1000 entities) and the result is 17.296 consuming a lot of memory and CPU. The good news is that deleteAll has been implemented well

We should add at least:

  1. updateAll (context, fieldName, value)
  2. updateAll (context, [fieldName : value])
  3. updateAll (context, fieldName, value, predicate)
  4. updateAll (context, [fieldName : value], predicate)

I think that with predicateBuilder we can remove the possibility to find/delete/update using attribute

Any suggestions?