0

I have a Firestore Array that has typing users (as IDs) in it. My goal is to remove the current User from that chat when the app terminates. I have read this thread where Frank van Puffelen said that you can remove values on disconnect by using this code:

ref.child("Clients").child(user.uid).child("current_venue").onDisconnectRemoveValue()

My problem is that this code is only able to remove a child document from the database and not an array. Is there any way to call onDisconnectRemoveValue() for a Firestore Array? Or is there any alternative for this case?

This is what my database structure looks like: Collection(Chats) -> Document(ChatID) -> Array(typingUsers)

Soni Sol
  • 2,367
  • 3
  • 12
  • 23
Oscar Junius
  • 320
  • 3
  • 10
  • A couple of issues. The question is tagged Firestore but the code is for the Realtime Database and they are not compatible. Secondly, onDisconnect only works with the RTDB. Your code mentions Firestore Array so... what's the implementation? Then, the question states you want to do something when the app terminates and that's very different than the app disconnecting. If you want to remove data when the app terminates (e.q. the user selects "quit") then why not just remove the data from Firebase when they tap/select quit? Can you clarify the question and what you're attempting to do? – Jay Dec 30 '21 at 19:48
  • Hey Jay, I am building a real time chat application with firebase right now. My chat data is stored in Firestore. Every time a user starts typing his userID is added to an array from that chat, so that the other user knows when he starts typing. My problem is that if you start typing and then the app terminates I don't know how to remove that value, better said: I don't know how the remove this array value in a more gentle way then just looping through all my chats and removing that user from the typing array. – Oscar Junius Jan 01 '22 at 10:19
  • Ok, I thought you may be doing something like that. Firestore doesn't support 'Presence' so it does not have a an onDisconnect function at all; but the RTDB does (it uses sockets). So the key here is so use both databases. Leverage the onDisconnect in the RTDB to fire off a Cloud Function to then take action with Firestore. There's actually a solution in the Cloud Firestore solutions section of the documentation [Build presence in Cloud Firestore](https://firebase.google.com/docs/firestore/solutions/presence) – Jay Jan 01 '22 at 14:26
  • Thanks for your answer, I'll take a look at that and come back to you – Oscar Junius Jan 01 '22 at 15:14

1 Answers1

0

You can remove whatever values you want from firebase in your appDelegate under the function:

func applicationWillTerminate(_ application: UIApplication) {
    // Remove the array
}
Trev347
  • 318
  • 1
  • 12