最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[已解决]swift中创建不在main主线程的定时器

iOS crifan 688浏览 0评论

swift中,之前定期去做事情,用的是NSTimer,定时器,但是NSTimer是必须在主线程中实现

-》占用主线程的资源和时间

-》现在想要去实现不在main主线程的,定期去做事情的功能

swift periodic timer

Using an NSTimer in Swift – Stack Overflow

ios – Swift – Do something every x minutes – Stack Overflow

ios – How can I use NSTimer in Swift? – Stack Overflow

NSTimer in Swift – We ❤ Swift

Dispatch Sources

swift periodic timer not in main thread

swift background timer

objective c – How do I create a NSTimer on a background thread? – Stack Overflow

swift – iOS Timer in the background – Stack Overflow

NSRunLoopCommonModes

iOS 中的timer — NSRunLoopCommonModes和Timer .NSThread和Timer.GCD中的Timer – Dev_APP的专栏 – 博客频道 – CSDN.NET

iOS知识点整理-RunLoop – 简书

NSRunLoopCommonModes和Timer – 简书

然后是可以创建的:

<code>    var wsKeepConnectTimer:dispatch_source_t

    init(){

        //self.wsKeepConnectTimer = NSTimer()
        //1. get/create a queue
        let backgroundQueue = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)
        //&lt;OS_dispatch_queue_root: com.apple.root.background-qos[0x52ceac0] = { xrefcnt = 0x80000000, refcnt = 0x80000000, suspend_cnt = 0x0, locked = 1, target = [0x0], width = 0x7fff, running = 0x1, barrier = 0 }&gt;
        //2. create source timer
        self.wsKeepConnectTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, backgroundQueue)
        //&lt;OS_dispatch_source: kevent-source[0x7c95fd70] = { xrefcnt = 0xa, refcnt = 0x3, suspend_cnt = 0x1, locked = 0, target = com.apple.libdispatch-manager[0x52cf040], ident = 0x0, mask = 0x0, pending_data = 0x0, registered = 0, armed = 0, deleted = 0, canceled = 0, needs_mgr = 0, timer = { target = 0x0, deadline = 0x0, last_fire = 0x0, interval = 0x0, flags = 0x2 }, kevent = 0x7c95f3f0, filter = DISPATCH_EVFILT_TIMER }&gt;

        super.init(nibName: nil, bundle: nil)
    }


    func startWsKeepConnectTimer() {
//        //Note: makesure when call NSTimer.scheduledTimerWithTimeInterval, need in Loop environment
//        //so here need in main queue
//        dispatchMain_async({
//            self.wsKeepConnectTimer = NSTimer.scheduledTimerWithTimeInterval(
//                NSTimeInterval(WebsocketKeepConnectIntervalInSec),
//                target: self,
//                selector:#selector(MainViewController.websocketKeepConnect),
//                userInfo: nil,
//                repeats: true)
//            NSRunLoop.currentRunLoop().addTimer(self.wsKeepConnectTimer, forMode: NSRunLoopCommonModes)
//
//            gLog.info(&quot;start websocket keep connect timer: \(self.wsKeepConnectTimer)&quot;)
//        })

   
        //create timer
        /*
         第1个参数: 需要给哪个定时器设置
         第2个参数: 定时器开始的时间/DISPATCH_TIME_NOW立即执行
         第3个参数: 定时器开始之后的间隔时间
         第4个参数: 定时器间隔执行的精准度, 传入0代表最精准(尽量的让定时器精准), 传入一个大于0的值, 代表多少秒的范围是可以接受的
         第四个参数存在的意义: 主要是为了提高程序的性能
         注意点: Dispatch的定时器接收的时间是纳秒
         */
        //3. set start time
        let startTime:dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, 0) //226016506822943
        //4. set interval
        let intervalInSec:UInt64 = WebsocketKeepConnectIntervalInSec * NSEC_PER_SEC //20000000000
        //5. set leeway
        let leeway:UInt64 = 0 * NSEC_PER_SEC //0
        dispatch_source_set_timer(self.wsKeepConnectTimer, startTime, intervalInSec, leeway)

        dispatch_source_set_event_handler(self.wsKeepConnectTimer, {
            self.websocketKeepConnect()
        })
       
        dispatch_resume(self.wsKeepConnectTimer)
    }

    func StopWsKeepConnectTimer() {
        gLog.info(&quot;stop websocket keep connect timer \(self.wsKeepConnectTimer)&quot;)
        //self.wsKeepConnectTimer.invalidate()
        dispatch_source_cancel(self.wsKeepConnectTimer)
    }
</code>

转载请注明:在路上 » [已解决]swift中创建不在main主线程的定时器

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
63 queries in 0.097 seconds, using 18.87MB memory