首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在swift中使用后台线程?

如何在swift中使用后台线程?
EN

Stack Overflow用户
提问于 2014-06-05 17:16:45
回答 17查看 318.7K关注 0票数 379

如何在swift中使用线程?

代码语言:javascript
运行
复制
dispatchOnMainThread:^{

    NSLog(@"Block Executed On %s", dispatch_queue_get_label(dispatch_get_current_queue()));

}];
EN

Stack Overflow用户

发布于 2022-02-25 14:52:32

下面的代码是否有缺点(当需要启动前台屏幕时)?

代码语言:javascript
运行
复制
import Foundation
import UIKit

class TestTimeDelay {

    static var connected:Bool = false
    
    static var counter:Int = 0

    static func showAfterDelayControl(uiViewController:UIViewController) {
        NSLog("TestTimeDelay", "showAfterDelayControl")
    }
    
    static func tryReconnect() -> Bool {
        counter += 1
        NSLog("TestTimeDelay", "Counter:\(counter)")
        return counter > 4
    }

    static func waitOnConnectWithDelay(milliseconds:Int, uiViewController: UIViewController) {
        
        DispatchQueue.global(qos: .background).async {
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.milliseconds(milliseconds), execute: {
                waitOnConnect(uiViewController: uiViewController)
            })
        }
    }

    static func waitOnConnect(uiViewController:UIViewController) {

        connected = tryReconnect()
        if connected {
            showAfterDelayControl(uiViewController: uiViewController)
        }
        else {
            waitOnConnectWithDelay(milliseconds: 200, uiViewController:uiViewController)
        }
     }
}   
票数 0
EN
查看全部 17 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24056205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档