2
我試圖等待從我的firebase數據庫檢索數據並將其存儲在字典中。 我想使用信號量這樣做,但我得到了一個NSURLSession/NSURLConnection HTTP加載失敗(kCFStreamErrorDomainSSL,-9806)錯誤 ,我試圖通過更改我的info.plist修復。但我沒有運氣。這是不是要走的路?等待firebase請求加載進行的問題
import UIKit
import Firebase
var done = false
class ListViewController: UIViewController, UICollectionViewDelegate {
let rootref = FIRDatabase.database().reference()
var item_dict = Dictionary<String,[String]>()
var item_img_dict = Dictionary<String,[String]>()
let semaphore = DispatchSemaphore(value: 0)
let queue = DispatchQueue.global()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "hello!"
print("before calling function==========================")
print("go in to request")
DispatchQueue.global(qos: .userInitiated).async{
self.rootref.child("items").observeSingleEvent(of: .value, with: { (snapshot) in
print("DOING SOMETHING IN FUNCTION !!!!")
for(type_2_container,item_list) in snapshot.value! as! Dictionary<String, AnyObject>{
for(uid_container, item_detail_container) in item_list as! Dictionary<String, AnyObject>{
for(uid, item_detail) in item_detail_container as! Dictionary<String, AnyObject>{
let item_detail_dict = item_detail as? Dictionary<String,String>
if self.item_dict.index(forKey: uid as! String) != nil{
self.item_dict[uid]?.append(item_detail_dict!["item_name"]!)
self.item_img_dict[uid]?.append(item_detail_dict!["item_image"]!)
}else{
self.item_dict[uid] = [item_detail_dict!["item_name"]!]
self.item_img_dict[uid] = [item_detail_dict!["item_image"]!]
}
}
}
}
self.semaphore.signal()
})
}
self.semaphore.wait()
print(self.item_dict)
print(self.item_img_dict)
}
在viewDidLoad中使用'self.semaphore.wait()'會阻塞主線程,這是一個非常糟糕的主意。你爲什麼不直接在firebase調用中使用完成關閉? – Michael
我對這一切都很陌生。你能解釋一下如何將它應用到我的代碼中嗎? –