我正在尝试将反应原生的应用投射到chromecast。 chromecast GCKDeviceScanner必须在主线程上运行。
react本机页面表示要在主线程上运行,你必须这样做:
- (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
}
关于如何在主线程上运行代码的任何想法?
答案 0 :(得分:4)
要在主线程中运行该代码部分,您可以执行以下操作:
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
}
}