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)

运行结果:

{wakeup port = 0x650b, stopped = false, ignoreWakeUps = true,
current mode = (none),
common modes = {type = mutable set, count = 1,
entries =>
2 : {contents = "kCFRunLoopDefaultMode"}
}
,
common mode items = (null),
modes = {type = mutable set, count = 1,
entries =>
2 : {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原理分析

线程间通讯参考

你可能感兴趣的:(Foundation-RunLoop)