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!

14
  • Instead of removeValue go to the appropriate index you want to delete and try setValue(nil) Commented Aug 29, 2017 at 3:28
  • I will try that, thanks man Commented Aug 29, 2017 at 3:32
  • let me know if it works and I can add it as an answer. Commented Aug 29, 2017 at 3:32
  • I tried it and nothing occurred, it did not change anything Commented Aug 29, 2017 at 3:35
  • I tried ref.child("Group Chats").child(key).child("sharers").child(did).setValue(nil) Commented Aug 29, 2017 at 3:36

3 Answers 3

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() 
    } 
    } 
    } 

    })
Sign up to request clarification or add additional context in comments.

1 Comment

well that actually will not work because there is difference between key and index
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);
   });

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.