like title. I use core data to insert item, i insert 100 items , it`s too slow. How to up the insert speed? Core Data whicher has transaction funcation?
[[<#NSManagedObjectContext#> undoManager] beginUndoGrouping];
... do some data modifications ....
[[<#NSManagedObjectContext#> undoManager] endUndoGrouping];
[[<#NSManagedObjectContext#> undoManager] undo]; // rollback
...
What you are looking for is to save
in Core Data after all 100 objects have been inserted as opposed to after each insert.
When objects are inserted into Core Data they are only present in memory. To persist your new objects to disc you should save which will take all changes (inserts, updates and deletes) and write them to disc together.
If you look at the documentation for -insertObject (below) you can read that inserting an object only registers the object for being inserted when changes are saved.
insertObject:
Registers an object to be inserted in the receiver’s persistent store the next time changes are saved.
- (void)insertObject:(NSManagedObject *)object
Parameters
object
A managed object.
By further looking at the documentation for -save: (below) you will se that it will (attempt to) save all the unsaved changed, in your case all 100 inserted items.
save:
Attempts to commit unsaved changes to registered objects to their persistent store.
- (BOOL)save:(NSError **)error
Parameters
error
A pointer to an
NSError
object. You do not need to create anNSError
object. The save operation aborts after the first failure if you passNULL
.Return Value
YES
if the save succeeds, otherwiseNO
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With