Foundation-RunLoop

  • 介绍

1.RunLoop 类提供一些接口管理输入源对象
2.RunLoop 对象处理像键盘,鼠标等事件,以及Port和NSConnection 对象,还处理Timer 事件

*使用须知

1.不能自己创建或者管理RunLoop对象,因为每个线程都会在需要的时候自动创建属于自己的RunLoop 对象,我们可以通过current()方法进入当前线程的run loop
2.RunLoop 没有考虑线程安全,所以前往不要在其他线程调用当前线程的RunLoop,会造成意想不到的错误


方法深入研究

  • a.获取当前线程
  let runloop = RunLoop.current
  print(runloop)

运行结果:

<CFRunLoop 0x608000362ac0 [0x10ed21c70]>{wakeup port = 0x650b, stopped = false, ignoreWakeUps = true,
current mode = (none),
common modes = <CFBasicHash 0x60800004ac20 [0x10ed21c70]>{type = mutable set, count = 1,
entries =>
2 : <CFString 0x10ecf98e0 [0x10ed21c70]>{contents = "kCFRunLoopDefaultMode"}
}
,
common mode items = (null),
modes = <CFBasicHash 0x60800004b8e0 [0x10ed21c70]>{type = mutable set, count = 1,
entries =>
2 : <CFRunLoopMode 0x6080001960a0 [0x10ed21c70]>{name = kCFRunLoopDefaultMode, port set = 0x6603, queue = 0x608000362d00, source = 0x6080001d9140 (not fired), timer port = 0x6803,
sources0 = (null),
sources1 = (null),
observers = (null),
timers = (null),
currently 499832817 (5030729704206) / soft deadline in: 1.8446739e+10 sec (@ -1) / hard deadline in: 1.8446739e+10 sec (@ -1)
},
}
}

  • b.RunLoop 转换成 CFRunLoop()
runloop.getCFRunLoop()
  • 给RunLoop 中添加一个定时器
DispatchQueue.global().async {
// 给线程起个名字
Thread.current.name = "异步线程"
let runloop = RunLoop.current
let timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in
          print(Thread.current.name)
 })
 runloop.add(timer, forMode: .commonModes)
 /// 必须调用一下run()方法,让其运行
 runloop.run()  
 }

运行:

Optional("异步线程")
Optional("异步线程")
Optional("异步线程")
Optional("异步线程")
Optional("异步线程")
...

  • 执行方法或者取消方法
    // 向RunLoop 中添加方法 order 代表执行顺序
    runloop.perform(#selector(self.input1), target: self, argument:"主线程", order: 2, modes: [RunLoopMode.commonModes])
    
    runloop.perform(#selector(self.input1), target: self, argument:"酷走天涯", order: 2, modes: [RunLoopMode.commonModes])
     runloop.perform(#selector(self.input2), target: self, argument:nil, order: 1, modes: [RunLoopMode.defaultRunLoopMode])
    
    /// 移除RunLoop 中 执行对象为Self ,参数为"酷走天涯" 的方法input1()
    runloop.cancelPerform(#selector(self.input1), target: self, argument: "酷走天涯")

下面是控制器自定义的几个方法

func input1(_ threadName:String){
    
    print(threadName + "酷走天涯1")
}

func input2(){
    
    print(Thread.current.name! + "酷走天涯2")
}

运行结果:

酷走天涯2
主线程酷走天涯1

提示:

在异步线程没有办法执行方法,具体原因不详

如果我们在异步 将这些方法添加到主线程的RunLoop 去是可以执行的

    DispatchQueue.global().async {
       Thread.current.name = "异步线程"
        
        let runloop = RunLoop.main
            runloop.perform(#selector(self.input1), target: self, argument:"主线程", order: 2, modes: [RunLoopMode.commonModes])
            
            runloop.perform(#selector(self.input1), target: self, argument:"酷走天涯", order: 2, modes: [RunLoopMode.commonModes])
            runloop.perform(#selector(self.input2), target: self, argument:nil, order: 1, modes: [RunLoopMode.defaultRunLoopMode])
            
            /// 移除RunLoop 中 执行对象为Self ,参数为"酷走天涯" 的方法input1()
            runloop.cancelPerform(#selector(self.input1), target: self, argument: "酷走天涯")
        
    }

还有下面这个方法可以使用

  func cancelPerformSelectors(withTarget target: Any)
  • 异步RunLoop 执行操作
  DispatchQueue.global().async {
        Thread.current.name = "异步线程"
        let runloop = RunLoop.current
                runloop.perform({
                    print(Thread.current.name! + "执行")
                })
        runloop.run()
        
    }

运行:

异步线程执行

  • 下面这个方法在线程间通讯时会用到
// 1.创建一个NSMachPort 对象 负责接收来自其它线程的消息
let port = NSMachPort()
port.setDelegate(self)
// 2.将port 对象加入到当前线程中去
RunLoop.current.add(port, forMode: .defaultRunLoopMode)

 // 其他线程和主线程行进通讯
 DispatchQueue.global().async {
      // 3.在其它线程创建一个port
      let myPort = NSMachPort()
      // 4.将port添加到当前线程
      RunLoop.current.add(port, forMode: .defaultRunLoopMode)
      let array = NSMutableArray(array: [123])
      // 5.开始通讯 注意port 
      port.send(before: Date(), msgid: 1, components: array, from: myPort, reserved: 0)
    }

让对象遵守协议NSMachPortDelegate

func  handleMachMessage(_ msg: UnsafeMutableRawPointer){
      print(msg)
}

运行:

0x00007fff58abe8c0

参考资料

RunLoop原理分析

线程间通讯参考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 最近离职了,可以尽情熬夜写点总结,不用担心第二天上班爽并蛋疼着,这篇的主角 RunLoop 一座大山,涵盖的...
    zerocc2014阅读 12,435评论 13 67
  • http://www.cocoachina.com/ios/20150601/11970.html RunLoop...
    紫色冰雨阅读 866评论 0 3
  • Runloop是iOS和OSX开发中非常基础的一个概念,从概念开始学习。 RunLoop的概念 -般说,一个线程一...
    小猫仔阅读 1,024评论 0 1
  • Run loop 剖析:Runloop 接收的输入事件来自两种不同的源:输入源(intput source)和定时...
    Mitchell阅读 12,480评论 17 111
  • 月黑风高的夜晚,房顶上有两个黑影,“满堂,你看看咱脚下的瓦,可是上等的琉璃瓦,你再看看...
    文番阅读 316评论 1 0