2017-03-15 14 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") 
    } 
}) 

나는 위의 코드를 테스트하지 않은,하지만 당신이 코드에서와 중포 기지 데이터베이스에서 작동하도록하기 위해 사소한 조작을 할 수 있어야합니다. 이 코드가 도움이 될 것입니다

1

스위프트 3 & & 중포 기지 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) 
    }