Realtime Updates with SnapshotListener - Firebase Firestore

Поділитися
Вставка
  • Опубліковано 6 вер 2024
  • In this video I will show you how you can get updates in realtime using firestore's snapshot listener.
    ⭐ Get certificates for your future job
    ⭐ Save countless hours of time
    ⭐ 100% money back guarantee for 30 days
    ⭐ Become a professional Android developer now:
    pl-coding.com/...
    Get the source code here:
    github.com/phi...
    Regular Android tutorials on my Instagram: / philipplackner_official
    Checkout my GitHub: github.com/phi...

КОМЕНТАРІ • 27

  • @spikeysanju5821
    @spikeysanju5821 4 роки тому +3

    Thanks a ton. Can you make a video about how we can achieve same with viewmodel coroutines flow with recycler view? Thanks in advance

  • @alltechpoint8508
    @alltechpoint8508 2 роки тому

    Assume, you have a document with the values x=3, y=”Hello” and z=10.0. A snapshot listener is attached to the collection this document is in. When will the snapshot listener trigger?
    A) If z is divided by 2
    B) After addSnapshotListener is called
    c) If the document is duplicated inside of that collection
    D) If x is increased by one
    E) If y is reversed
    F) If the document is retrieved by a client
    which options will be the answers according to me all of these will be answer because every time listener will trigger.

  • @Berk45632
    @Berk45632 4 роки тому +2

    In the for loop of retrievePersons(), the code is document in querySnapshot.documents.
    But in the for loop of subscribeToRealtimeUpdates(), it is document in it, where it refers to querySnapshot. Shouldn't that be it.documents?

  • @Berk45632
    @Berk45632 4 роки тому +4

    Don't forget to add X Æ A-XII as a name to the database 😂😂

  • @raghavaggarwal8644
    @raghavaggarwal8644 3 роки тому

    Hi Philipp, u said that snapshot listener is a strong point of Firestore as it immediately reflects any change in database but as i know same is the case with realtime database, it also immediatly reflects any change in database. Then how Firestore > realtime?

  • @mataal8770
    @mataal8770 Рік тому

    Hi Thanks for this video. Still have some problem How do you add then afterwards delete without having to refresh your fragment or activity? I'm having an issue wherein after listening for "add" events then the products are added to my activity or fragment then doing a delete or update doesn't work. I need to either refresh the fragment or the activity so the changes happen. If I do one CRUD operation linear they will work like consecutively adding products or consecutively doing updates or deletes. But doing one operation then the other alternately doesn't work.While searching on the net for any helpful resource, I stumbled upon a ListenerRegistration class which I can call listener registration.remove() on the instance but still cant get around this problem.

  • @marcinmazur1803
    @marcinmazur1803 3 роки тому +1

    Hey, great video ;) is there any elegant way of implementing snapshot listener with the mvvm pattern? I mean where should I keep implementation of the firebase listener? Directly on the view model? Or should I rather pass the live data to the repository from the view model? I don't think that repository should know anything about the view model... Helph

    • @PhilippLackner
      @PhilippLackner  3 роки тому +1

      I'd do that in the repo and use a callback function. In the viewmodel you can then set the livedata value when that callback function triggers

    • @marcinmazur1803
      @marcinmazur1803 3 роки тому

      @@PhilippLackner thank you kindly

    • @vladopris1172
      @vladopris1172 3 роки тому

      @@PhilippLackner I understand how we can listen for any changes in the remote db firestore, but how can we also update the local cache? I mean if I change an object from firebase console, I want that change to be persisted in the client too at the same object.

  • @tolgaoren6012
    @tolgaoren6012 2 роки тому

    thank man, its helpful

  • @mattgraves3709
    @mattgraves3709 3 роки тому

    Hey Phillip, how does LiveData work in this scenario?
    Would I even use it?
    I am actually doing this a little differently, the Firestore functions are in a separate class called ContactCollection which is called from my ViewModel to create a single source of information that is observed from the fragment and survives redraw.

    • @PhilippLackner
      @PhilippLackner  3 роки тому

      you could have a callback function from the snapshot listener in the repo that you provide for the viewmodel and then whenever that callback triggers, you post the corresponding value into your live data object

    • @fatihkoc1520
      @fatihkoc1520 Рік тому

      @@PhilippLackner if we want to get realtime updates from repository to our viewmodel we need to use lambda functions right ? is there any other solutions i heard about flows can returns multiple values from repository to viewmodel what do you think philip i dont know

  • @mattgraves3709
    @mattgraves3709 3 роки тому

    Mwahahahhaha!!!
    I am so powerful,
    My data updates itself!!

  • @abhishekjagushte1806
    @abhishekjagushte1806 4 роки тому

    Hey is it efficient to use this is a chats app which has local Database as storage, so the incoming data is stored in Room and to implemented message delivered and seen thing

  • @ravisorathiya1756
    @ravisorathiya1756 3 роки тому +1

    Its like livedata

  • @leandrosistemavs1043
    @leandrosistemavs1043 4 роки тому

    Search in recycled view , please!

  • @hanadanefa7837
    @hanadanefa7837 4 роки тому

    is this cost wise if implemented?

    • @PhilippLackner
      @PhilippLackner  4 роки тому

      That totally depends on how often your data updates

    • @hanadanefa7837
      @hanadanefa7837 4 роки тому

      @@PhilippLackner thanks, may I know how can I detach or stop the firestore listener using the your code in the video in the realtime update?

    • @iamsurajgiri
      @iamsurajgiri 4 роки тому +1

      @@hanadanefa7837 create a val snapShotlistner = yout listner then add this listner to your collection to retrieve data but in onStop or anywhere you want just call yourcollection.removeSnapshotListner() ... contact me @iamsurajgiri (IG,Twitter,Facebook,Github) for any questions

    • @hanadanefa7837
      @hanadanefa7837 4 роки тому

      @@iamsurajgiri can you make a sample? I mean what is the listener? or how can I make a global variable that I put below so that I can call the detachListener val on onStop method
      val detachListener = myCollectionRef
      .whereEqualTo("myField", value)
      .limit(1)
      .addSnapshotListener { querySnapshot, e ->
      e?.let {
      Log.w("Error", e.message, e)
      return@addSnapshotListener
      }
      querySnapshot?.let {
      if (it.isEmpty) {
      // do something
      } else {
      try {
      for (document in it) {
      // get all document
      }
      } catch (e: Exception) {
      }
      }
      }
      }

    • @iamsurajgiri
      @iamsurajgiri 4 роки тому

      @@hanadanefa7837 dm me on instagram @iamsurajgiri