2017-03-15 8 views
1

Firebaseデータベースの値をstatusで更新する際に問題が発生しました。私はそれがautoIDである子(id)にアクセスする方法を理解できません。 Swift 3 Entityに属性を設定する必要がありますか?または、子供(autoID)または類似のものがありますか?私はFirebaseとSwift 3で新しくなっています。これは私のコードです:更新値Firebase Swift 3

self.ref?.child("requests").queryOrdered(byChild: "Sender").queryEqual(toValue: self.items[indexPath.row].Sender).setValue("2", forKey: "status")) 

click here to see an image showing my database

答えて

0

.observe内にコードを移動してみてください。これにより、ステータスを取得し、それが適切な子にアクセスしていることを確認することができます。下の子パスを使用すると、statusの値は上書きされますが、子の中の残りの値は上書きされません。

self.ref?.child("requests").queryOrdered(byChild: "Sender").queryEqual(toValue: self.items[indexPath.row].Sender).observe(.value, with: { (snapshot) in 

    let value = snapshot.value as? NSDictionary 

    if value != nil { 
     print(value) 
     print(value["status"]) 
     // If status is what you want, and prints out the correct status and 
     // correct child item 

     // Update the status 
     self.ref.child("requests/\(self.items[indexPath.row].Sender)/status").setValue("2") 
    } 
}) 

私は上記のコードをテストしていませんが、あなたはそれがあなたのコードの中で、あなたfirebaseデータベース上で動作させるために微調整を行うことができるはずです。

1

スウィフト3 & & Firebase 3

このコードはあなたを助けることを願っています...

// create the reference you want to observe 
    let myRequestRef = FIRDatabase.database().reference().child("requests").queryOrdered(byChild: "Sender").queryEqual(toValue: self.items[indexPath.row].Sender) 

    // check the sender value 
    myRequestRef.observeSingleEvent(of: .value, with: { (snapshot) in 

     //get the sender key 
     let senderKey = snapshot.key as String 

     // create status ref for new status value 
     let statusRef = FIRDatabase.database().reference().child("requests").child(senderKey) 

     // create new dict for status value 
     let newValue = ["Status": 2] as [String: Any] 

     statusRef.updateChildValues(newValue, withCompletionBlock: { (error, _) in 
      if error != nil { 
       print(error?.localizedDescription ?? "Failed to set status value") 
      } 
      print("Successfully set status value") 
      // Update your UI 
      DispatchQueue.main.async { 
       // Do anything with your UI 
      } 
     }) 
    }) { (error) in 
     print("Failed to get snapshot", error) 
    } 
関連する問題