如何在后台加载数据时使用 UI 事件

发布于 2024-10-24 05:07:46 字数 1207 浏览 4 评论 0原文

我有一个 iPhone 小应用程序,可以从 Web 服务加载数据。为了确保加载数据时不会出现任何问题,我在应用程序上创建了一个半透明视图,并使用 CFRunloopRun() 等待所有数据在后台加载。这是代码:

        self.connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];

    // Now show an animation
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    UIView *window = [[UIApplication sharedApplication] keyWindow];
    UIView *shield = [[UIView alloc] initWithFrame:window.bounds];
    shield.backgroundColor = [UIColor blackColor];
    shield.alpha = 0.5f;
    [window addSubview:shield];
    spinner.center = shield.center;
    [shield addSubview:spinner];
    spinner.hidden = NO;
    NSLog( @"JCL.callServerWithRequest(), spinner view: %@, shield view: %@, window: %@", spinner, shield, window );
    [spinner startAnimating];

    // Hand over to the Runnloop to wait
    CFRunLoopRun();

    [spinner stopAnimating];
    [spinner removeFromSuperview];
    [spinner release];
    [shield removeFromSuperview];
    [shield release];

这工作正常,只是在加载后播放某处按钮的任何点击,因此如果用户点击下载按钮两次,他也会下载两次。

知道如何在移除防护罩之前使用 UI 事件。

谢谢-安迪

I have a little iPhone app that loads data from a web service. To make sure that nothing goes wrong while loading the data I create a semi-transparent view over the app and use CFRunloopRun() to wait until all the data is loaded in the background. This is the code for that:

        self.connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];

    // Now show an animation
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    UIView *window = [[UIApplication sharedApplication] keyWindow];
    UIView *shield = [[UIView alloc] initWithFrame:window.bounds];
    shield.backgroundColor = [UIColor blackColor];
    shield.alpha = 0.5f;
    [window addSubview:shield];
    spinner.center = shield.center;
    [shield addSubview:spinner];
    spinner.hidden = NO;
    NSLog( @"JCL.callServerWithRequest(), spinner view: %@, shield view: %@, window: %@", spinner, shield, window );
    [spinner startAnimating];

    // Hand over to the Runnloop to wait
    CFRunLoopRun();

    [spinner stopAnimating];
    [spinner removeFromSuperview];
    [spinner release];
    [shield removeFromSuperview];
    [shield release];

This works fine except that any clicks on a button somewhere is played after the loading so if the users clicks on the download button twice he will do the download twice as well.

Any idea how to consume the UI events before the shield is removed.

Thanks - Andy

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

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

发布评论

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

评论(2

耳钉梦 2024-10-31 05:07:46

尝试一下,不要弄乱运行循环。我怀疑 UI 事件正在进入窗口上的正常循环,但在自定义循环返回之前不会被处理,此时“屏蔽”视图不再捕获它们。如果您将防护罩放在适当的位置,然后让主运行循环处理事情,则防护罩应该正常捕获它们。

Try it without messing with runloops. I suspect that the UI events are coming in to the normal loop on the window but not being processed until your custom loop returns, at which point the "shield" view is no longer there to catch them. If you put the shield in place and then let the main runloop handle things, the shield should catch them all as normal.

别忘他 2024-10-31 05:07:46

感谢 Anomie,我最终尝试不使用 CFRunLoopRun(),这非常困难,因为执行分为两部分: - 调用和通过回调返回结果。但后来我搬起石头砸了自己的脚,因为我试图阻止返回线程以减慢执行速度,但这不起作用,因为它在主线程中再次执行。

最终我确实减慢了 Web 服务的速度,然后一切都按预期进行。

Thanks to Anomie I finally tried out to go without the CFRunLoopRun() and it is quite difficult because the execution is split into two parts: - the Invocation and the Return of the Result through a callback. But then I shot myself in the proverbial foot because I tried to block the returning thread to slow down the execution which did not work because that was executed again in the main thread.

Eventually I did slow down the Web Service and then everything worked as expected.

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