2017-12-13 6 views
0

firebase structure신속 중포 기지 중첩 된 아이들은 내가 모든 그룹에서 특정 사용자 (currentUserId)을 삭제해야 볼 수있는 중포 기지 구조에서

를 삭제 사전을

###########################UPDATED################################### 
 

 
let groupsRef = self.root.child("groups") 
 
groupsRef.observeSingleEvent(of: .value, with: { snapshot in 
 
    
 
    for groupChild in snapshot.children { 
 
     let groupSnap = groupChild as! DataSnapshot 
 
     var dict = groupSnap.value as! [String: Any] 
 
      let uid = dict["utenti"] as! [String: Bool] 
 
     for each in uid {     
 
      if each.key == self.currentUserID{ 
 
       print(each.key) 
 
       
 
     //i now need a way to remove this key:value 
 

 
      } 
 
      
 
      } 
 
      
 
     
 
     
 
    } 
 
    
 
})

나는 그래서 내가 DIT의 모든 키를 추출에 더 갈 수 새로운 아니에요 해요 cionary, 나는 내가 삭제해야하는 것과 비교할 것이고, 같은 것이면 나는 삭제할 것입니다. 누군가 도움을 줄 수 있습니까?

답변

0
  let groupsRef = self.root.child("groups") 
        groupsRef.observeSingleEvent(of: .value, with: { snapshot in 

         for groupChild in snapshot.children { 
          let groupSnap = groupChild as! DataSnapshot 
          let groupKey = groupSnap.key 
          //added a groupKey to track the id of each group 
          var dict = groupSnap.value as! [String: Any] 
          var uid = dict["utenti"] as! [String: Bool] 

          //then for each key:value in uid check if is there a key = to currentuserID 
          for each in uid { 

           if each.key == self.currentUserID{ 
            uid.removeValue(forKey: each.key) 
            //here you remove currentuserId from the dictionary and below 
           //finally you set back the new value of the dictionary without currentuserId 

        self.root.child("groups").child(groupKey).child("utenti").setValue(uid) 

           } 
          } 
         }  
        }) 
+0

작동합니다 !! thnx 그래서 mutch! – Marco

0

당신은 사전에 걸쳐 루프

for (key, value) in uid { 


} 

를 사용할 수 있습니다.

하지만 실제로는, 신속의 공식 문서에서 찾고 당신에게 올바른 대답을 줄 것이다 ...

0
let groupsRef = self.root.child("groups") 
      groupsRef.observeSingleEvent(of: .value, with: { snapshot in 

       for groupChild in snapshot.children { 
        let groupSnap = groupChild as! DataSnapshot 
        for subGroupChild in groupSnap.children { 
         //Here you need to remove that specific user if condition match 
         //specificUserUID is that user's id that user to be deleted 
         if let snapref = subGroupSnap as! DatabaseReference { 
         snapref.queryOrdered(byChild: "utenti").queryEqual(toValue: specificUserUID).observe(.childAdded, with: { (snapshot) in 

         snapshot.removeValue(completionBlock: { (error, reference) in 
           if error != nil { 
            print("There has been an error:\(error)") 
           } 
         }) 
         }) 
         } 
        } 
       } 

      }) 
귀하의 코드는 그냥 사용자 ID로 사용자 thaat 찾을 필요가 위와 같이 가서 특정 스냅 샷을 삭제합니다

.

+0

thnx하지만 "오류 유형 : 'Any'유형의 값에 'queryOrdered'멤버가 없습니다." – Marco