1
我知道有這樣的問題,但它不能像我在Swift 2
中那樣工作。我希望有人能澄清,這樣我就可以像過去那樣運作!我在Swift 2
有這個代碼,它會檢測到變化firebase
,更新表中的數據,然後重新加載只改變了指數:dispatch_get_main_queue在Swift 3中不能正常工作
DataService.ds.REF_USERS.observeEventType(.ChildChanged, withBlock :{
(snapshot) in
if initialUserLoad == false {
for (index,user) in self.usersArray.enumerate() {
if user.objectForKey("uId") as! String == snapshot.key {
self.usersArray[index] = snapshot.value as! NSDictionary
dispatch_async(dispatch_get_main_queue()){[unowned self] in
self.collectionView.reloadItemsAtIndexPaths([NSIndexPath(forItem: index, inSection: 0)])
}
}
}
}
})
的Swift 3
代碼自動更新它改成這樣:
DataService.ds.REF_USERS.observe(.childChanged, with :{
(snapshot) in
if initialUserLoad == false {
for (index,user) in self.usersArray.enumerated() {
if user.object(forKey: "uId") as! String == snapshot.key {
self.usersArray[index] = snapshot.value as! NSDictionary
DispatchQueue.main.async{[unowned self] in
self.collectionView.reloadItems(at: [IndexPath(item: index, section: 0)])
}
}
}
}
})
現在我得到的錯誤是:
終止應用程序由於未捕獲的異常「NSInternalInconsistencyException」,理由是:「嘗試刪除伊特從第0部分,其中只包含3個項目更新'
因此,似乎有一個線程問題。任何人都知道我可以如何使它像以前一樣工作?預先感謝您提供的任何幫助。
這不是一個線程問題。很顯然,錯誤地給出了「理由:」試圖從第0節中刪除第4項,它只包含更新前的3個項目「。您正嘗試刪除比該部分中的總項目更大的索引處的項目。 –
你檢查了堆棧跟蹤嗎?哪條線路崩潰?設置異常斷點 – Paulw11
嘗試重新加載單元時崩潰 –