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];
}