I have a solution with Xamarin Android, Xamarin iOS and WPF projects.
There is a lot of multithreading activity mainly in the ViewModels and data access components, we use INotifyDataErrorInfo in our model and of course XAML DataBinding with ObservableCollection lists.
What I need is an ObservableCollection implementation for all the platforms that will make the list concurrent to make sure that only one thread can modify the collection.
In my research I found couple of implementations with this one the closest to what I want, but uses Thread and reflection that are not available in a Portable Class Library. http://pastebin.com/hKQi6EHD . I guess modifying and abs
Any sources available to get me to the right track?
I stumbled on this same issue when working on a similar problem. I can up with 2 possible solutions, 1 use a concurrent collection for your repository and a facade to update the observable collection. 2 create your own ConcurrentObservable collection. 2 is risky because you need to get the sync correct or it will affect performance. My thoughts are you could just wrap the ConcurrentCollection and then implement INotifyPropertyChanged. I would think a hash or some type of efficient comparison should be done before raising the event. You could also make your own INotifyPropertyChangedAsync event. Just my thoughts.
Adapter:
ConcurrentQueue _concurrentQueue = new ConcurrentQueue<object>();
Add(object o)
{
_concurrentQueue.Enqueue(o);
if (!_updateStatus)
{
Task.Run(() => UpdateBindingCollection()).ConfigureAwait(true);
}
}
On enter of the UpdateBindingCollection _updateStatus = true signaling that we already have an update in the works.
void UpdateBindingCollection()
{
while (_concurrentQueue.Count > 0)
{
object o;
_concurrentQueue.Dequeue(out o);
_observableCollection.Add(o);
}
}
Easier but blocking:
Lock(_observableCollection)
{
//Perform update either with add or range.
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.