Question How to have multiple Firebase AddSnapshotListener(s) inside a single class?

VivekScorp

Member
Joined
May 12, 2022
Messages
11
Programming Experience
3-5
I have a class in android which has a listener setup to get visitorlist updates.

C#:
var visitorDetailsCollection = FirebaseFirestore.Instance.Collection("visitorDetails");
            var visitorDetailsquery = visitorDetailsCollection.WhereEqualTo("UserID", Firebase.Auth.FirebaseAuth.Instance.CurrentUser.Uid);
            visitorlistner = visitorDetailsquery.AddSnapshotListener(this);

and when the firestore db is updated the following method runs and gets the updates

C#:
public void OnEvent(Java.Lang.Object obj, FirebaseFirestoreException error)
{
try
            {
                var docs = (QuerySnapshot)obj;
                visitorDetails.Clear();
                foreach (var doc in docs.Documents)
                {
                        var visitordetails = new VisitorDetails
                        {
                            Name = doc.Get("VisitorName").ToString(),
                            ContactNumber = doc.Get("VisitorContact").ToString(),
                            VehicleNumber = doc.Get("VisitorVehicleNo").ToString(),
                            IsApproved = doc.Get("VisitorIsApproved").ToString(),
                            IsActive = doc.Get("VisitorIsActive").ToString(),
                            Purpose = doc.Get("PurposeOfVisit").ToString(),
                            Email = doc.Get("VisitorEmail").ToString(),
                            FromDate = toDatetime(doc.GetDate("FromDate") as Date),
                            ToDate = toDatetime(doc.GetDate("ToDate") as Date),
                            docID = doc.Id
                        };
                        visitorDetails.Add(visitordetails);                   
}

Now I want to add another listener to get residentdetails like this

C#:
residentlistner = residentDetailsquery.AddSnapshotListener(this);

How do I tell this listener to run a different function like

C#:
public void OnEvent2(Java.Lang.Object obj, FirebaseFirestoreException error)
{}
when the firestore db is updated?
 
The response you got in Stack Overflow is the best, cleanest approach: pass in a different instance instead of this. Any other approach, would be hacky where you need to detect the object passed into the event.
 
The same way you would do would any other class. Declare a class. Call new to create an instance of that class. I don't understand why this is a difficult concept. You should have learned this as a basic part of learning how to program in any object-oriented language. I would assume that you already know how to do this considering that you are already advanced enough to be writing a mobile application. Please tell me that you aren't doing cargo-cult programming.
 
ha ha, But that way it will again create a new instance of the same class and a new instance of the same onEvent function right?

Coz in Stackoverflow Jason asked me to create a separate instance of the listener rather than using "this"
so I asked on how to do that actually :)
 
No, you'll need to create an instance of a different class that also has an OnEvent() method on it.

Basically in pseudo code:
C#:
class VisitorDetailsUpdatedEventArgs : EventArgs
{
    // Properties/fields to hold visitor detail docs
    // Constructor that takes docs
}

class VisitorDetailsListener : WhatEverFirebaseBaseClassOrInterfaceNeeded
{
   :
    public event EventHandler<VisitorDetailsUpdateEventArgs> VisitorDetailsUpdated;

    public void OnEvent(Java.Lang.Object obj, FirebaseFirestoreException error)
    {
        // your code here that extracts visitor details docs

        // Notify event listeners the C# way, rather than the Java way. Pass the docs to into the event args.
        VisitorDetailsUpdated?.Invoke(this, new VisitorDetailsUpdatedEventArgs( ... ))
    }
    :
}

class ResidentUpdatedEventArgs : EventArgs
{
    // Properties/fields to hold resident docs
    // Constructor that takes docs
}

class ResidentListener : WhatEverFirebaseBaseClassOrInterfaceNeeded
{
:
    public event EventHandler<ResidentUpdatedEventArgs> ResidentUpdated;

    public void OnEvent(Java.Lang.Object obj, FirebaseFirestoreException error)
    {
        // your code here that extracts resident docs

        // Notify event listeners the C# way, rather than the Java way. Pass the docs to into the event args.
        ResidentUpdated?.Invoke(this, new ResidentUpdatedEventArgs( ... ))
    }
:
}

class YourController
{
    :
    VisitorDetailsListener _visitorDetailsListener = new VisitorDetailsListener();
    ResidentListener _visitorDetailsListener = new ResidentListener();
    :

    public YourController( ... )
    {
        :
        // register for events the C# way
        _visitorDetailsListener.VisitorDetailsUpdated += OnVisitorDetailsUpdated;
        _residentListener.VisitorDetailsUpdated += OnResidentUpdated;
        :
        // register for Firebase events
        visitorDetailsQuery.AddSnapshotListener(_visitorDetailsListener);
        residentQuery.AddSnapshotListener(_residentListener);
        :
    }

    // Handle events the C# way

    void OnVisitorDetailsUpdated(object sender, VisitorDetailsUpdatedEventArgs e)
    {
        // process the visitor details in the event args
    }

    void OnResidentUpdated(object sender, ResidentUpdatedEventArgs e)
    {
        // process the resident in the event args
    }
    :
}
Lines 46-47 above create new instances of C# classes. Lines 50-51 register those classes as listeners for Firebase.
 
See post #4 and #6. You don't need to copy and paste someone else's code. You just need to understand and think. You can chose to get a fish, or you can choose to learn how to fish.
 
Back
Top Bottom