Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove string value from array in firebase

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!

like image 339
Randy Windin Avatar asked Jun 05 '26 06:06

Randy Windin


1 Answers

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

    })
like image 163
3stud1ant3 Avatar answered Jun 07 '26 23:06

3stud1ant3