Reza
Reza

Reputation: 5634

Akavache and collectionChanged event

(1) I am having trouble getting the CollectionChanged event of an ObservableCollection to fire whilst using Akavache, here is the code I have (simplified)

            GraphCollection = new ObservableCollection<UserData>();

        _cache.GetOrFetchObject(TabType.Graph.ToString(),
                async () => await _dataStore.GetAllDocuments(TabType.Graph))
            .Subscribe(
                GraphList =>
                {
                    GraphCollection = new ObservableCollection<UserData>(GraphList);
                    //GraphCollection.Shuffle();
                    NoGraphItems = GraphCollection.Count == 0;
                });
            
        GraphCollection.CollectionChanged += (sender, args) =>
        {
            NoGraphItems = GraphCollection.Count == 0;
        };

Ideally, when I Add/Delete data I want to trigger the event to check to see if the collection is empty and then assign a bool property that it is or isn't empty.

I am making simple Add/Delete like so, and then calling a RefreshCache method to invalidate the data and recreate the data, not sure if this is the most efficient way to do it as well.

                    var graphRecord = GraphCollection.FirstOrDefault(x => x.Id == data.Id);
                GraphCollection.Remove(dreamRecord);

                RefreshCache(TabType.Graphs, DreamsCollection);


    private void RefreshCache(TabType tabType, ObservableCollection<UserData> collection)
    {
        _cache.InvalidateObject<UserData>(tabType.ToString());
        _cache.InsertObject(tabType.ToString(), collection); 
    }

(2) I am not currently setting the DateTime offset, do I need this? Can someone give me an example of how to write it out, the docs don't clearly state this.

DateTimeOffset? absoluteExpiration

Upvotes: 0

Views: 49

Answers (1)

Jason
Jason

Reputation: 89204

your Subscribe creates a new instance of GraphCollection so the event handler that was assigned to the original instance no longer works with the new instance

try this instead

GraphList =>
{ 
    GraphCollection = new ObservableCollection<UserData>(GraphList);
    NoGraphItems = GraphCollection.Count == 0;
    GraphCollection.CollectionChanged += // handler goes here
});

Upvotes: 0

Related Questions