none
Sorting values of ObservableCollection (Reactive Extensions) in WinRT

    Question

  • In my application i am having a list with more than 400 push pins. For avoiding the UI block i am using the following terminolagy along with Rx framework.

    var observer = localStoreCollection.ToObservable();
    observer.Subscribe(StoresOnNext, StoresOnError, StoresOnCompleted);

    On the OnNext i will add this pushpins one by one to my binding list. Along with the map i am populating a list with the same collection. So i need to sort this list based on the nearest location. So my question is how i can sort this collection without re assigning the list. (Kind of stuff similar to call by reference).

    NB : Also the assigning of distance is made inside the OnNext method.

    Wednesday, June 19, 2013 3:07 PM

Answers

  • Hi,

    Note that ToObservable uses CurrentThreadScheduler by default, which in your example will cause Subscribe to block and StoresOnNext will be called synchronously for each item.

    You could pass in a concurrency-introducing scheduler to ToObservable; however, I believe that WinRT has multiple UI threads, so blocking the current thread while processing each item may be fine.  Therefore, try not using observables in the first place.

    > my question is how i can sort this collection without re assigning the list

    Re-assigning the list to what?  Are you simply looking for an in-line sorting algorithm?  What does that have to do with Rx?

    - Dave


    http://davesexton.com/blog

    • Marked as answer by StezPet Tuesday, June 25, 2013 10:44 AM
    • Edited by Dave Sexton Tuesday, June 25, 2013 6:17 PM Changed <del> tag to <s> tag.
    Friday, June 21, 2013 1:25 AM

All replies

  • Hi,

    Note that ToObservable uses CurrentThreadScheduler by default, which in your example will cause Subscribe to block and StoresOnNext will be called synchronously for each item.

    You could pass in a concurrency-introducing scheduler to ToObservable; however, I believe that WinRT has multiple UI threads, so blocking the current thread while processing each item may be fine.  Therefore, try not using observables in the first place.

    > my question is how i can sort this collection without re assigning the list

    Re-assigning the list to what?  Are you simply looking for an in-line sorting algorithm?  What does that have to do with Rx?

    - Dave


    http://davesexton.com/blog

    • Marked as answer by StezPet Tuesday, June 25, 2013 10:44 AM
    • Edited by Dave Sexton Tuesday, June 25, 2013 6:17 PM Changed <del> tag to <s> tag.
    Friday, June 21, 2013 1:25 AM
  • I have not done any work on WinRT (mostly WPF and Silverlight) so I hope this is useful.

    If you are adding large numbers of values to a collection one-at-a-time (e.g. Observe on Dispatcher) you may find that this can be slower than batching up small groups of values and adding them to the list. Firing one at a time can "flood the dispatcher", in which case the program spends more time processing messages from the dispatcher queue than really doing any work.

    With regards to sorting, you may find that you can use a CollectionViewSource that will sort the Bound collection for you. 

    Neither of these really have a lot to do with Rx as Dave points out. Using Rx however, can help ever so slightly in the concurrency/async operations you may need, but it appears that you currently dont have any?

    Lee


    Lee Campbell http://LeeCampbell.blogspot.com

    Monday, June 24, 2013 8:03 AM