简体   繁体   English

Swift Xcode6 UIActivityIndi​​catorView缓慢显示

[英]Swift Xcode6 UIActivityIndicatorView Slow to display

How do I get the UIActivityIndicatorView to display first, then execute other code? 如何使UIActivityIndi​​catorView首先显示,然后执行其他代码?

I've experimented with using sleep, and it works but it doesn't "feel" right and adds an extra second to processing a bunch of core data stuff. 我已经尝试过使用睡眠,并且可以使用它,但是它“感觉不到”,并且在处理大量核心数据方面增加了额外的时间。 I've also tried dispatching it to the main thread which only works some of the time. 我还尝试将其分派到仅在某些时间工作的主线程。 (I'm guessing when the rest of the block is executed outside of the main thread). (我猜该块的其余部分何时在主线程之外执行)。

Ideally as soon as a user touches the button the instance of the UIActivityIndicatorView would display (which seems to happen where I've used it in other apps by itself or with other minimal processing). 理想情况下,只要用户触摸按钮,UIActivityIndi​​catorView的实例就会显示出来(这似乎发生在我在其他应用程序中单独使用它或进行最少的处理的情况下)。

Details: I have an IBAction connected to a button that executes a bunch of core data stuff, sometimes including images, that takes between 1 - 3 seconds to finish. 详细信息:我将IBAction连接到一个按钮,该按钮执行一堆核心数据,有时包括图像,这些过程需要1-3秒才能完成。 When it finishes it dismisses the view controller. 完成后,关闭视图控制器。 The view controller where this is executed is presented as a modal over current context. 执行此操作的视图控制器显示为当前上下文的模态。

Here is a code snippet: 这是一个代码片段:

// get the background queue
let bg_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

dispatch_async(bg_queue, {
    // long running code here...

    dispatch_async(dispatch_get_main_queue(), {
        self.activityIndicator.stopAnimating()
    })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM