Reputation: 1286
I would like to know if I could have information about saving progress of NSManagedObjectContext.
if (![self.context save:&error])
{
NSLog(@"Unresolved error %@", error);
[self.context rollback];
return NO;
}
I need to save a lot of stuff so the saving could take a lot of time... I need to inform the user about the progress in percentage.
Upvotes: 2
Views: 777
Reputation: 28409
The method easiest is to just make changes to the MOC in small chunks, saving each small change. However, in order to do a true percentage you have to know the total amount of work in the first place... or make a good guess and update the total as you go.
Anyway, you could do something like this...
dispatch_async(some_queue, ^{
while (workToDo) {
getSomeWorkToDo();
doTheCurrentPieceOfWork();
// Save current piece of work practicing Safe Core Data
[managedObjectContext performBlock:^{
if (![managedObjectContext save:&error]) {
// Handle error
}
}];
computePercentageComplete();
dispatch_async(dispatch_get_main_queue(), ^{
// Update the control with the new percent complete
});
}
});
Upvotes: 1
Reputation: 14886
Unfortunately there isn't really a process for Core Data saves and it varies from device to device - saves on 4S is much faster than 3GS for example.
What you may be able to do is do several saves. I did this for one project. I was looping through a lot of XML and for every 20 loops I would save the context and update a progress bar. This made the whole parsing + saving process seem a bit faster visually. It may actually also help speed things up because saving a small chunk of data at a time was faster for me than saving all the changes to the context at the end. I ended up with a magic number of 17 saves in my loop, which was the fastest parsing time for me, so you can play around with the number a bit.
You can also just show an activity view (spinner) which is an indefinite way or providing visual feedback to a user on the saving process.
Upvotes: 2