我正在使用示例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读取。我怎么能这样做?