2016-09-29 83 views
4

工作更新的Xcode 8和轉換斯威夫特2項目進入迅捷3之後,其他代碼工作得很好,但runTransactionBlock不工作,發現這個錯誤:火力地堡runTransactionBlock不迅速3

"runTransactionBlock: usage detected while persistence is enabled. Please be aware that transactions will not be persisted across app restarts"

可能是什麼錯誤?

火力地堡runTransaction代碼

postRef.runTransactionBlock({ (currentData: FIRMutableData) -> FIRTransactionResult in 
      if var post = currentData.value as? [String : AnyObject], let uid = FIRAuth.auth()?.currentUser?.uid { 
       var stars : Dictionary<String, Bool> 
       stars = post["likeMap"] as? [String : Bool] ?? [:] 
       var likeCount = post["likeCount"] as? Int ?? 0 
       if let _ = stars[uid] { 
        // Unstar the post and remove self from stars 
        likeCount -= 1 
        self._liked = false 
        stars.removeValue(forKey: uid) 
       } else { 
        // Star the post and add self to stars 
        likeCount += 1 
        self._liked = true 
        stars[uid] = true 
       } 
       post["likeCount"] = likeCount as AnyObject? 
       post["likeMap"] = stars as AnyObject? 
       self._likeCount = likeCount 
       // Set value and report transaction success 
       currentData.value = post 

       return FIRTransactionResult.success(withValue: currentData) 
      } 
      return FIRTransactionResult.success(withValue: currentData) 
     }) { (error, committed, snapshot) in 
      if let error = error { 
       print(error.localizedDescription) 
      } 
     } 

回答

5

不知道哪個火力地堡SDK版本您使用,但我會爲你提供我以前runTransactionBlock成功一個基本的例子:

數據庫

enter image description here


代碼

let ref = FIRDatabase.database().reference() 
let refReservations = ref.child("reservations") 


refReservations.runTransactionBlock { (currentData: FIRMutableData) -> FIRTransactionResult in 
    if var data = currentData.value as? [String: Any] { 
    var count = data["count"] as? Int ?? 0 
    count += 1 
    data["count"] = count 

    currentData.value = data 
    } 

    return FIRTransactionResult.success(withValue: currentData) 
} 

我用火力地堡SDK版本:3.0.1,您可以使用此方法從您的代碼此信息:FIRDatabase.sdkVersion()

+0

我的火力SDK版本也是3.0.1 – Jelly

2

那消息是警告您的應用程序正在使用事務正在將數據保存到磁盤。

當您啓用磁盤持久性時,應用程序觀察到的(最近)數據會持久保存到磁盤。這樣,如果用戶在沒有網絡連接的情況下重新啓動應用程序,數據將可用。

在這種斷開狀態下,火力地堡客戶端也將存儲所有的本地的寫操作到磁盤中所謂未決寫入隊列。然後,當網絡連接再次可用時,Firebase客戶端會將所有掛起的寫入發送到服務器,當然也會收到服務器爲其提供的任何更新。

但是,雖然斷開連接,但Firebase客戶端將而不是將事務保留到磁盤。由於該組合可能不符合您的預期,Firebase會記錄您看到的警告。

0

我有同樣的問題,我找到了解釋和解決方案。

來自Firebase文檔的示例對於Swift 3來說是錯誤的,因爲它表示來自數據庫的FIRMutableData對象的類型是[String:AnyObject],反之它應該是[String:Any]。

原因是響應實際上是[String:Dictionary],而Dictionary不是AnyObject(任何可以表示任何類型的實例,包括函數類型和可選類型 AnyObject可以表示任何類的實例類型)。因爲Dictionary不是一個類。

爲了讓該代碼工作,你必須更換這行代碼:

if var post = currentData.value as? [String : AnyObject], let uid = FIRAuth.auth()?.currentUser?.uid { 

這一行:

if var post = currentData.value as? [String : Any], let uid = FIRAuth.auth()?.currentUser?.uid {