如何在ios中创建自己的NSRunLoop?

时间:2015-12-28 12:14:47

标签: ios swift nsrunloop

我正在使用示例poc在不同的runloop中运行EASession而不是使用NSRunLoop.currentRunLoop。我怎样才能实现它?

这是我的代码..

ViewController.swift

   let myRef = myClass()
   func viewDidLoad() {
      let status = myRef.openSession()
      print(status)
   }

在NSObject类myClass.swift

let handler = myHandler()
var isOpened = false
func openSession()-> Bool {
  myHandler._open()

// HERE I WANT TO WAIT TO CHECK THE RESPONSE   ****

  return isOpened
}

func myCustCallBack() {
   isOpened = true
}

Handler.swift

_open() {
var _accessory: EAAccessory? = nil
        for obj in accessories {
            if obj.protocolStrings.contains("protocolString") {
                _accessory = obj
                break
            }
        }

        if (_accessory != nil) {

            session = EASession(accessory: _accessory!, forProtocol: "protocolString")
            if (session != nil) {
                session?.inputStream?.delegate = self
                session?.inputStream?.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
                session?.inputStream?.open()

                session?.outputStream?.delegate = self
                session?.outputStream?.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
                session?.outputStream?.open()
// HERE WE ARE USING THE NSRunLoop.currentRunLoop() - SO SAME UI THREAD
            }

}

在会话回调中,当HasSpaceAvailable事件触发写入一些数据时以及HasBytesAvailable事件触发时调用我自己的委托方法myCustCallBack()

****在这一行我必须等到我从ExternalAccessory读取。我怎么能这样做?

0 个答案:

没有答案