PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 11, 2022

[FIXED] How to make the message visible on the recipient's device instantly - SwiftUI and FirebaseFirestore

 July 11, 2022     chat, firebase, google-cloud-firestore, message, swiftui     No comments   

Issue

I make ChatApp by SwiftUI-FirebaseFirestore. I run 2 simulators at the same time and if user A sends a message to user B, I have to reload user B's interface to display. I want it to be automatic and show up immediately so what's the idea to do?


Solution

What you're looking for is called a snapshot listener. Effectively it's treated the same as with any other documents but it has a closure that allows you to respond to updated events on a particular document. It's general usage is this.

db.collection("cities").document("SF")
.addSnapshotListener { documentSnapshot, error in
  guard let document = documentSnapshot else {
    print("Error fetching document: \(error!)")
    return
  }
  guard let data = document.data() else {
    print("Document data was empty.")
    return
  }
  print("Current data: \(data)")
}

You should be able to look at the document snapshot to determine any changes in data, then respond to those changes. In your case you would reload the view.

https://firebase.google.com/docs/firestore/query-data/listen



Answered By - xTwisteDx
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing