ObservableCollection count resets to 0 from second function call

VivekScorp

Member
Joined
May 12, 2022
Messages
11
Programming Experience
3-5
There is a Dependency class in android that has these two functions

1.
C#:
public void OnEvent(Java.Lang.Object obj, FirebaseFirestoreException error)
        {
try
            {
                var docs = (QuerySnapshot)obj;
                visitorDetails.Clear();
                foreach (var doc in docs.Documents)
                {
                    Console.WriteLine(doc.Get("VisitorName").ToString());
                        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);
                }
                Console.WriteLine("onevent - " + visitorDetails.Count.ToString());
                HasReadCompleted = true;               
                MessagingCenter.Send<object, string>(this, "HasChanges", "true");
            }
            catch (FirebaseFirestoreException error1)
            {
                Console.WriteLine(readMode + error1.Message);
                HasReadCompleted = false;
            }
}
2. public async Task<ObservableCollection<VisitorDetails>> ReadVisitorDetails()
        {
            try
            {                                             
                Console.WriteLine("return - " + visitorDetails.Count.ToString());
                return visitorDetails;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }
        }
In my PCL ViewModel class
C#:
 MessagingCenter.Subscribe<object, string>
                (this, "HasChanges", (sender, status) =>
                {
                    if(status=="true" && userdetails != null)
                    {
                        details = await DatabaseHelper.ReadVisitorDetails();
                    }               
                });
so my PCL class calls ReadVisitorDetails() whenever the MessagingCenter text is true

The first time when it runs the output is fine onevent - 1 return - 1

after the first time return is always 0 onevent - 1 return - 0

visitorDetails is a class variable initialized inside the constructor
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,251
Location
Chesapeake, VA
Programming Experience
10+
visitorDetails is a class variable initialized inside the constructor
I assume that visitorDetails was not declared static. If it was, then you could potentially run into race conditions unless you are doing appropriate locking.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,251
Location
Chesapeake, VA
Programming Experience
10+
No. It should not be static. I was just trying to make sure that you did not declare it as static.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,251
Location
Chesapeake, VA
Programming Experience
10+
At this point, the only thing I can think of is to add more debugging information into your code to make sure that you are talking to the correct instance of the dependency object. Something like this in pseudo-code:
C#:
class MyDependencyObject : ...
{
    public Guid ObjectId { get; private set; }
    public ObservableCollection<VisitorDetails> visitorDetails = new ObservableCollection<VisitorDetails>();
    :

    public MyDependencyObject()
    {
        ObjectId = new Guid();
        :
    }

    public void OnEvent(Java.Lang.Object obj, FirebaseFirestoreException error)
    {
        :
        Console.WriteLine($"onevent - {visitorDetails.Count} - {ObjectId}");
        :
    }

    public async Task<ObservableCollection<VisitorDetails>> ReadVisitorDetails()
    {
        :
        Console.WriteLine($"return - {visitorDetails.Count} - {ObjectId}");
        :
    }

    :
}

And somewhere in your view model something like:
C#:
Console.WriteLine($"HasChanges subscription - {DatabaseHelper.ObjectId}");
 

VivekScorp

Member
Joined
May 12, 2022
Messages
11
Programming Experience
3-5
onevent - 4-00000000-0000-0000-0000-000000000000
ObjectID in PCL - 00000000-0000-0000-0000-000000000000
ObjectID in PCL - 00000000-0000-0000-0000-000000000000
return - 0-00000000-0000-0000-0000-000000000000
[chatty] uid=10154(com.vivekscorp.aucms) identical 2 lines
return - 0-00000000-0000-0000-0000-000000000000
Thread finished: <Thread Pool> #11
The thread 0xb has exited with code 0 (0x0).

onevent - 4-00000000-0000-0000-0000-000000000000
ObjectID in PCL - 00000000-0000-0000-0000-000000000000
ObjectID in PCL - 00000000-0000-0000-0000-000000000000

onevent - 4-00000000-0000-0000-0000-000000000000
ObjectID in PCL - 00000000-0000-0000-0000-000000000000
ObjectID in PCL - 00000000-0000-0000-0000-000000000000
return - 0-00000000-0000-0000-0000-000000000000
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,251
Location
Chesapeake, VA
Programming Experience
10+
My fault. I keep forgetting that new Guid() doesn't actually create a new random Guid. You'll want something like Guid.NewGuid().
 

VivekScorp

Member
Joined
May 12, 2022
Messages
11
Programming Experience
3-5
onevent - 4-f8023eb6-f8c9-41d5-a01c-6c65959b86db
ObjectID in PCL - f8023eb6-f8c9-41d5-a01c-6c65959b86db
return - 4-f8023eb6-f8c9-41d5-a01c-6c65959b86db

onevent - 4-f8023eb6-f8c9-41d5-a01c-6c65959b86db
ObjectID in PCL - f8023eb6-f8c9-41d5-a01c-6c65959b86db
return - 0-f8023eb6-f8c9-41d5-a01c-6c65959b86db

onevent - 4-f8023eb6-f8c9-41d5-a01c-6c65959b86db
ObjectID in PCL - f8023eb6-f8c9-41d5-a01c-6c65959b86db
return - 0-f8023eb6-f8c9-41d5-a01c-6c65959b86db
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,251
Location
Chesapeake, VA
Programming Experience
10+
Thanks for the update. That is strange. What is clearing out that collection?

Unfortunately, the next step would be to wrap that collection with your own implementation where you can put in tracing and breakpoints. :-(

Alternatively you can also register for notifications on that observable collection and get notifications when the content changes. You could then put tracing or breakpoints in your notification handlers to see when it gets cleared out changed.
 

VivekScorp

Member
Joined
May 12, 2022
Messages
11
Programming Experience
3-5
Hi SkyDiver,
I resolved the issue. There was a property
C#:
private ObservableCollection<VisitorDetails> details;
        public ObservableCollection<VisitorDetails> Details { 
            get { 
                return details; 
            } 
            set 
            { 
                details = value;
                OnPropertyChanged("Details");
            } 
        }
in my PCL class and I previously set it to details.Clear(); inside the ProcessData function removing that resolved the issue.
I still don't know how the two are connected.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,251
Location
Chesapeake, VA
Programming Experience
10+
Yay!
 
Top Bottom