iOS4网络ActivityIndi​​catorVisible通过UI被阻止

发布于 2024-10-05 19:11:38 字数 743 浏览 3 评论 0原文

我正在请求下载一些图像,然后我想用它们替换子视图。 这意味着 UI 被阻止,稍后会显示新视图。

我希望用户明白,阻塞是由于下载而发生的。

首先,我尝试使用

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[self downloadFunction];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

That 导致活动指示器最大程度地闪烁(几乎没有),因为我的请求阻塞了 UI 线程。

我无法将 downloadFunction 置于后台,因为我依赖于在推送控制器之前可用的下载数据(会导致错误“不支持多次推送相同的视图控制器实例”,因为我可以单击按钮不止一次)。

然后我尝试在视图顶部放置一个带有旋转轮的子视图:

[self performSelectorInBackground:@selector(showActivitySubView) withObject:nil];
or  
[NSThread detachNewThreadSelector: @selector(showActivitySubView) toTarget:self withObject:nil];

但 UI 仍然被阻止,并且我的指示器在下载完成后才显示...

有什么想法吗?

I am doing a request to download some images then I want to replace the subview with them.
This means that the UI is blocked and then the new view displays sometime later.

I want the user to understand that the blocking occurs due to the download.

First I tried to use

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[self downloadFunction];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

That leads to maxium a blink (mostly nothing) of the Activity Indicator, since my request is blocking the UI thread.

I cannot put the downloadFunction to the background, since I depend on the downloaded data to be available before i push the controller (would lead to the Error "Pushing the same view controller instance more than once is not supported" since I could click the button more than once).

Then I tried to put a subview with a spinning wheel on top of the view:

[self performSelectorInBackground:@selector(showActivitySubView) withObject:nil];
or  
[NSThread detachNewThreadSelector: @selector(showActivitySubView) toTarget:self withObject:nil];

but still the UI is blocked and my indicator is just displayed after the download finished...

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦归所梦 2024-10-12 19:11:38

您可以简单地将方法分成两部分,并使用定时调用来允许 UI 刷新线程在阻塞主线程之前启动。例如:

- (void) doActualWork {
  [self downloadFunction];
  [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void) doWork {
  [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  [self performSelector:@selector(doActualWork) withObject:Nil afterDelay:0.05];
}

可能有更好的方法来做到这一点,我不确定任务栏上的小指示器是否足以让应用程序审查人员满意的活动指示,但这应该可行并且不需要线程。

You can simply split your method in two and use a timed call to allow the UI refresh thread to pick up before you block the main thread. For example:

- (void) doActualWork {
  [self downloadFunction];
  [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void) doWork {
  [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  [self performSelector:@selector(doActualWork) withObject:Nil afterDelay:0.05];
}

There's probably a better way of doing this, and I'm not sure that the little indicator on the task bar would be enough activity indication to keep the app review folks happy, but this should work and won't require threading.

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