我正在尝试将本机应用程序投射到chromecast。 chromecast GCKDeviceScanner必须在主线程上运行。
react native页面说要在主线程上运行,您必须执行以下操作:
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
我对swift或ios不太熟悉,所以该代码放在哪里,以及如何使用它?我有我的方法:
let filterCriteria = GCKFilterCriteria(forAvailableApplicationWithID: "myApp")
let deviceScanner = GCKDeviceScanner(filterCriteria: filterCriteria)
if let deviceScanner = deviceScanner {
deviceScanner.addListener(self)
deviceScanner.startScan()
deviceScanner.passiveScan = true
}
关于如何在主线程上运行代码的任何想法?
最佳答案
要在主线程中运行该代码段,可以执行以下操作:
DispatchQueue.main.async {
let filterCriteria = GCKFilterCriteria(forAvailableApplicationWithID: "myApp")
let deviceScanner = GCKDeviceScanner(filterCriteria: filterCriteria)
if let deviceScanner = deviceScanner {
deviceScanner.addListener(self)
deviceScanner.startScan()
deviceScanner.passiveScan = true
}
}
另一个选项是RunLoop.main.perform { .. }
。旧版本,在Swift 4之前(甚至是3?):
dispatch_async(dispatch_get_main_queue()) {
let filterCriteria = GCKFilterCriteria(forAvailableApplicationWithID: "myApp")
let deviceScanner = GCKDeviceScanner(filterCriteria: filterCriteria)
if let deviceScanner = deviceScanner {
deviceScanner.addListener(self)
deviceScanner.startScan()
deviceScanner.passiveScan = true
}
}
关于ios - 在react-native中在主线程上运行Swift代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39232647/