views:

48

answers:

1

Hello,

I am trying to improve performance by limiting my objects in memory by using a cache.

This creates problem with my ObservableCollection. Well, i think the code speaks for itself. I have "transformed" my ObservableCollection-derivative to this:

public class TrackCollection : INotifyPropertyChanged, IEnumerable {
    private readonly List<Guid> _guids = new List<Guid>();

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info) {
        if (PropertyChanged != null) {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    public void Add(Track track) {
        _guids.Add(track.Id);
    }

    public void Add(Guid trackId) {
        _guids.Add(trackId);
    }

    public IEnumerator GetEnumerator() {
        var tracks = new List<Track>(_guids.Count);
        foreach(Guid id in _guids)
            tracks.Add(MediaCache.Instance.GetTrack(id));

        return tracks.GetEnumerator();
    }
}

When i call the Add-methods i would like to call my NotifyPropertyChanged on the class it self.

I have a feeling that this is the wrong approach to this kind of problem, it feels like a workaround.

Anyways, is this the correct solution, and if so, how should i proceed?


The cache method looks like this:

    private readonly List<Track> _tracks = new List<Track>();
    public Track GetTrack(Guid id) {
        foreach (var track in _tracks.Where(track => track.Id == id))
            return track;

        _tracks.Add(Database.Database.Instance.GetTrack(id));
        return _tracks[_tracks.Count - 1];
    }
A: 

If you want your collection to be observable by the binding system, you need to implement INotifyCollectionChanged. This will allow you to notify interested clients about changes in your collection.

On a side note, to notify listeners about a change in the whole class, you can notify about a change to a property with null passed for the name. This will cause all bindings to reevaluate the source.


I looked through the code a bit further and I'm confused about your intention. Every enumeration returns a brand new collection, which doesn't mesh well with the notion of an observable collection. The clients of an observable collection expect the individual instances of their items to stay consistent, so they only need to respond to restructuring, rather than refreshing the whole list. Could your clarify what you're trying to accomplish?


Instead of trying to handle your caching in the collection class, perhaps you can create a 'reference' class TrackReference and use a standard ObservableCollection. The class would look something like:

public class TrackReference
{
    private Guid _id;

    public Track Track
    {
        get { return MediaCache.Instance.GetTrack(id); }
    }

    public TrackReference(Track track)
    {
        _id = track.Id;
    }

    public TrackReference(Guid trackId)
    {
        _id = trackId;
    }
}

Now you have a lightweight handle you can pass around (and observe in a collection) that gives you access to the full Track data when you're ready for it. This is also a good place to add any lightweight meta-data (track name, perhaps?) for your UI classes to Bind.

Dan Bryant
As you can see, i have implemented the INotifyPropertyChanged interface. I will try to pass null as the property name.
Erik
I want to store all my Track-objects in one place. I have updated the first post. Yes, i didn't think about that.
Erik