数据导入CoreData时如何显示进度条?
我正在将大量数据导入 CoreData。我想显示导入进度的进度条。
首先,我显示进度条。 然后我正在计算要导入的数据。 然后我设置了一个 for 循环:
- 在这个 for 循环中我首先设置 coreData 对象。
- 然后我增加进度视图。
在此循环之后,我将保存 coreData 对象并隐藏进度条。
[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];
(简化)
问题是,进度条没有更新。它显示,但在 for 循环完成后首先更新,然后隐藏。因此,进度条仅在 for 循环之后不久显示。
有什么想法如何解决这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在另一个线程中进行导入,并使用
-[NSObject PerformSelectorOnMainThread:withObject:waitUntilDone:]
将进度信息发送到主线程。享受调试并发问题的乐趣。
您可能还可以使用 CATransactions 做一些可怕的事情,但我的印象是 UIKit 应该为您处理所有这些。
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.
您是否尝试启用线程动画:
Apple 的更多内容 文档。
Did you try enabling threaded animation with:
More in Apple's documentation.
显示进度后一秒钟后调用核心数据方法,如下所示:
保存数据后,从该函数中消除进度:
Call core data method after a second after showing progress like this way:
And once data get saved, dismiss progress from that func: