How to show progressBar during data import into CoreData?
I'm importing a large amount of Data into CoreData. I want to 开发者_如何转开发show a progressBar for the progress of importing.
First I'm showing the progress bar. Then I'm counting the data I want to import. Then I setup a for loop:
- in this for loop I first set the coreData object.
- then i'm incrementing the progress view.
After this loop I'm saving the coreData object and hiding the progressBar.
[self.progressView show];
int allFiles = [file count];
int currentFile = 1;
for(NSString *trackid in file) {
[entidyDesc setName:[track objectForKey:@"theKey"]];
float progress = [self convertAmountForProgressBar:currentFile maxNum:allFiles];
self.progressView.progressBar.progress = progress;
}
[self.progressView hide];
[self.managedObjectContext save:nil];
(simplified)
The problem is, the progressBar is not updating. It showing, but gets updated first after the for loop is completed and than its hiding. So, the progress bar is only showing up short after the for loop.
Any ideas how to solve this problem?
Do the import in another thread and send progress information to the main thread with -[NSObject performSelectorOnMainThread:withObject:waitUntilDone:]
.
Have fun debugging concurrency issues.
You might also be able to do something terrible with CATransactions, but I'm under the impression that UIKit is supposed to handle all of those for you.
Did you try enabling threaded animation with:
- (void)setUsesThreadedAnimation:(BOOL)flag
More in Apple's documentation.
Call core data method after a second after showing progress like this way:
DispatchQueue.main.async { [self] in
SVProgressHUD.show()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now())
{
coreDataMethod()
}
}
And once data get saved, dismiss progress from that func:
SVProgressHUD.dismiss()
精彩评论