3

I am having a problem removing a kid string from an array of other aid strings in firebase. When a user creates a group chat, they include sharers. Those sharers are added to an array of strings and then added to firebase when made. Now when a user clicks the leave group chat button, I can't figure out a way to remove the current user's user id from that array, in which I have already checked if their uid is in the array. I have tried everything I can guys, first I tried ref.child("Group Chats").child(groupKey).child("sharers").child(myriad).removeValue, and that did not work, I also tried creating a new array without the current users id, removing the old array and adding the new one to firebase, all did not work, now I turn to some fellow coders. How can I remove the current users did from an array of uids in firebase.

        let action = UIAlertAction(title: "Leave Group Chat", style: .default, handler: {( alert : UIAlertAction) -> Void in
                let ref = FIRDatabase.database().reference()
                    if let keyp = self.groupChats[indexPath.row].key {

                        ref.child("Group Chats").child(keyp).child("sharers").child(uip).removeValue()



                                              }
                    self.groupChat.removeAll()
                    self.pull()
                    self.pullFromSharers()
                    self.tableViewManage.reloadData()

                })

Firebase

Thank you!

aofdev
  • 1,772
  • 1
  • 15
  • 29
Randy Windin
  • 187
  • 2
  • 12

3 Answers3

2

After discussion with OP, we have found the solution

Problem is that your path is not calculated correctly, your path was getting the value instead of child, so modify your code as follows:

 ref.child("Group Chats").child(keyp).child("sharers").observe(.value, with: { snapshot in 
    if let sharers = snapshot.value as? [String] { 
    for i in 0..<sharers.count { 
    if sharers[i] == uip { 
      ref.child("Group Chats").child(keyp).child("sharers").child("\(i)").removeValue() 
    } 
    } 
    } 

    })
3stud1ant3
  • 3,586
  • 2
  • 12
  • 15
0

i used this simple method in angularjs. i think you also need same thing. try this it will help you.

var url = "your db path is here"
var dataObj = {'sharers':''};
   ref(url).set(dataObj).then(function(res) {
      console.log("response:- ",res);
   });
Harish Mahajan
  • 3,254
  • 4
  • 27
  • 49
0

One way is to remove the entire node by setting to nil like I said in my comment and then setting the node back to the parent. Please check this post as well about firebase arrays.

https://stackoverflow.com/a/40657455/5123516

Moaz Khan
  • 1,272
  • 1
  • 13
  • 28