数据导入CoreData时如何显示进度条?

发布于 2024-10-03 04:44:54 字数 709 浏览 6 评论 0原文

我正在将大量数据导入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别想她 2024-10-10 04:44:54

在另一个线程中进行导入,并使用 -[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.

岛歌少女 2024-10-10 04:44:54

您是否尝试启用线程动画:

- (void)setUsesThreadedAnimation:(BOOL)flag

Apple 的更多内容 文档

Did you try enabling threaded animation with:

- (void)setUsesThreadedAnimation:(BOOL)flag

More in Apple's documentation.

怪我入戏太深 2024-10-10 04:44:54

显示进度后一秒钟后调用核心数据方法,如下所示:

DispatchQueue.main.async { [self] in
           
            SVProgressHUD.show()
           
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now())
            {
                coreDataMethod()
            }
            
        }

保存数据后,从该函数中消除进度:

SVProgressHUD.dismiss()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文