Windows Support Number

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Saturday, 5 March 2011

WP7Contrib: Trickling data to a bound collection

Posted on 13:04 by Unknown
Recently Rich & I've had problems with adding items to list control where the data template is chocked full of XAML goodies - triggers, opacity masks, images, overlay, loads of things I know little about... ;) The culmination of all this UI goodness when used with a bound list control is the blowing of the '90 mb limit' for WP7 devices. As I posted before, we knew the problem wasn't with the data model so we knew it was the data template, now there is plenty of coverage of issues with data templates in Silverlight and this post it not adding to this list, its about a technique to reduce the memory footprint once you've done as much as you can with the data template.

So we're developing an app which exhibits high memory usage, now obviously we want to reduce the memory consumption for the app so we trimmed down the data template for the list control, but we're still seeing the 90 mb limit exceeded for relative few items. Our data set could in theory contain several hundred items but it was failing for less than 40 items and this is an issue.

So Rich can up with the idea of adding a delay between each add to the bound collection - trickle the items, so after some experimentation we discovered the use of a call to the garbage collector when all the items have been added along with a delayed between each item reduced the memory usage to an acceptable level.

The initial implementation Rich came up with used a DispatcherTimer and I thought I could improve on this with Rx (reactive extensions) - this didn't work out (Rich - I won't try and be smart again). Simply the Rx added to much overhead and this caused  the rendering to be less smooth than the DispatcherTimer implementation. So we went back to the DispatcherTimer.

We've provider the implementation in the WP7Contrib, there are currently 2 implementations:

  • TrickleUniqueToCollection<T> - Only adds items not already in the collection,
  • TrickleAllToCollection<T> - adds all items irrespective if they already exist in the collection.

Both classes implement the interface ITrickleToCollection<T> via the base class BaseTrickleToCollection<T>. It defines a set of methods that allow the starting, pausing, resuming and stopping of trickling as required. The start method defined the time delay between each add as well the source collection and the destination collection.

public interface ITrickleToCollection<T>
{
   bool Pending { get; }
   bool IsTrickling { get; }
   void Start(int trickleDelay, IEnumerable<T> sourceCollection, IList<T> destinationCollection);
   void Stop();
   void Suspend();
   void Resume();
}

Both classes use a functional style via the Action method to notify the caller when an action occurs, e.g. when the trickling has started, stopped, item added etc. Shown below is the constructor signatures for both implementations:

public sealed class TrickleUniqueToCollection<T> : BaseTrickleToCollection<T>
{
   public TrickleUniqueToCollection(Action addedAction, Action completedAction)
      : this (addedAction, () => {}, () => {}, () => {}, () => {}, completedAction)

   public TrickleUniqueToCollection(Action addedAction, Action startedAction, Action stoppedAction,
                                    Action suspendedAction, Action resumedAction, Action completedAction)
   : base (addedAction, startedAction, stoppedAction, suspendedAction, resumedAction, completedAction)
}

public sealed class TrickleAllToCollection<T> : BaseTrickleToCollection<T>
{
   public TrickleAllToCollection(Action addedAction, Action completedAction)
      : this (addedAction, () => {}, () => {}, () => {}, () => {}, completedAction)

   public TrickleUniqueToCollection(Action addedAction, Action startedAction, Action stoppedAction,
                                    Action suspendedAction, Action resumedAction, Action completedAction)
   : base (addedAction, startedAction, stoppedAction, suspendedAction, resumedAction, completedAction)
}

Each class has a specific implementation for the DispatcherTimer, shown below is the implementation for the TrickleAllToCollection<T>, the interesting detail is the timer being shutdown once trickling all of the items has completed - no point leaving it running consuming device resources (memory & CPU).

public TrickleAllToCollection(Action addedAction, Action startedAction, Action stoppedAction,
                              Action suspendedAction, Action resumedAction, Action completedAction)
   : base (addedAction, startedAction, stoppedAction, suspendedAction, resumedAction, completedAction)
{
      this.DispatcherTimer = new DispatcherTimer();
      this.DispatcherTimer.Interval = this.StartupDelay;
      this.DispatcherTimer.Tick += delegate
         {
            if (this.DispatcherTimer.IsEnabled)
            {
               if (this.Source.Count != 0)
               {
                  var item = this.Source.Dequeue();
                  this.Destination.Add(item);
                  this.AddedAction();

                  if (this.Count == 0)
                     this.DispatcherTimer.Interval = this.Delay;

               this.Count++;
               }
               else
               {
                  this.DispatcherTimer.Stop();
                     this.CompletedAction();
               }
            }
         };
   }

Using these classes is simply, just instantiate an instance with the required Action methods:

 this.trickler = new TrickleUniqueToCollection<Property>(() => this.UpdateTrickleStatus(TrickleStatus.Added),
                                                        () => this.UpdateTrickleStatus(TrickleStatus.Started),
                                                        () => this.UpdateTrickleStatus(TrickleStatus.Stopped),
                                                        () => this.UpdateTrickleStatus(TrickleStatus.Suspended),
                                                        () => this.UpdateTrickleStatus(TrickleStatus.Resumed),
                                                        () => this.UpdateTrickleStatus(TrickleStatus.Completed));

And then start trickling to the bound destination collection:


   this.trickler.Start(432, result.Properties, this.aggregatedPropertiesView);

That's pretty much it, as I said these are in the WP7Contrib and can be found in the Collections project. I've also created a demo application 'TrickleDemo' in the Spikes directory of the code base. This demo doesn't show any memory problems with data templates, it only demonstrates how to use this trickle to collection pattern.

Next time a post about how we do RESTful communication in WP7 using the HttpWebRequest  & Reactive Extensions...
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in CodePlex, WP7, WP7Contrib | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Unit testing Rx methods Timeout & Retry with moq
    Earlier this week I was trying to unit test an asynchronous service (Foo) which used another asynchronous service (Bar) internally and ran i...
  • Understanding RefCount in Reactive Extensions
    A couple of weeks ago  @LordHanson  & I ran into an issue converting a stateless async service exposed as an Rx cold observable to a  co...
  • StructureMap: ILifecycle
    The other day I wanted to control the scope of a service inside a web based app with semantics which didn't fit either 'HttpContextS...
  • MVVM anti-pattern: Injecting the IoC container into a View Model
    This is another anti-pattern I've seen a lot recently, the dynamic use of the IoC container inside a view model to resolve child view mo...
  • How many pins can Bing Maps handle in a WP7 app - part 1
    part2 -  http://awkwardcoder.blogspot.com/2011/10/how-many-pins-can-bing-maps-handle-in.html part3 -  http://awkwardcoder.blogspot.com/2011/...
  • Bad developers love 'The Daily WTF'
    When 'The Daily WTF' started up back in 2003/2004 it was a great laugh looking at shocking code other developers wrote, but after a ...
  • Using CompositeDisposable in base classes
    To help make an object eligible for collection by the GC (garbage collector) one would implement the IDisposable interface. Executing the di...
  • Implementing a busy indicator using a visual overlay in MVVM
    This is a technique we use at work to lock the UI whilst some long running process is happening - preventing the user clicking on stuff whil...
  • Daily Dilbert Service - the most important service I've ever written...
    NuGet package available here ... First off a big shout to  @hamish  &  @leeoades  on this one - I'm just blogging about it. At work ...
  • Comparing performance of .Net 4.5 to .Net 4.0 for WPF
    Currently I'm working on a .Net 4.0 WPF app and we've had some discussion about moving to .Net 4.5, we don't get to make the dec...

Categories

  • .Net
  • .Net 4.5
  • Abstractions
  • Advertising
  • Agile
  • Agile Courage
  • AOP
  • Async
  • automated testing
  • Azure
  • Azure IIS RESTful development
  • BDD
  • Bing Maps
  • Bounded Context
  • C#
  • C# 5.0
  • Caching
  • Chocolatey
  • CLoud
  • CodePlex
  • Coding
  • Coding Building CI Testing
  • Coding C#
  • coding C# IoC StructureMap
  • Coding Functional-Programming
  • Coding REST Knowledge
  • Coding Services
  • Coding TDD Refactoring Agile
  • Command
  • continuous testing
  • coupling
  • CultureInfo
  • DAL
  • databases
  • DDD
  • DDD Coaching
  • DDD Domain Events Auditing nHibernate
  • DDD Entities Value Objects
  • Debugging
  • Design Patterns
  • Design Patterns Databases Auditing
  • Developement
  • Development
  • Development Coding
  • Development Process
  • Development unit testing
  • Development VS 2011
  • Diagnostics
  • Disposable
  • Exceptions
  • FINDaPAD
  • FindaPad Property Rental Windows Phone 7 Mobile Devices
  • Fun Coding Duct-Tape
  • Hotfixes
  • integration testing
  • IoC
  • jasmine
  • javascript
  • Jobs Development
  • LINQ
  • marketplace
  • Mobile Devices
  • Mocking
  • MSDN Coding
  • MSpec
  • Multilingual
  • MVC
  • MVVM
  • nCrunch
  • nHbiernate Repository Pattern Criteria
  • nHibernate Auditing Design Fluent
  • nHibnerate Entities Events Listeners
  • node.js
  • nodes.js
  • Nokia
  • NoSQL RavenDB Azure Development
  • Observations
  • OO
  • ORM
  • Performance
  • Portable Class Library
  • Portable Library
  • PostSharp
  • Process
  • Rants
  • RavenDB IIS 7.5 Development
  • Reactive
  • Reactive Extension
  • Reactive Extensions
  • ReadOnlyCollections
  • Resharper
  • REST Distributed-Systems
  • REST HTTP
  • rest web
  • RESTful
  • Rx
  • Serialization
  • Silverlight
  • Silverlight Installation
  • Task
  • TDD
  • TDD IoC DI
  • TDD Mocking
  • TDD Team Observation
  • Telerik
  • testing
  • threading
  • TPL
  • UI
  • Undo-Redo
  • unit testing
  • ViewModels
  • VS 2012
  • wcf
  • web api
  • Web Services
  • web services mobile devices data
  • WebAPI
  • Windows
  • Windows 8
  • windows phone
  • Windows Phone 7
  • WP7
  • WP7 Bing Maps Development Network HTTP
  • WP7 Bing Maps Development UK Crime
  • WP7 Bing Maps Development UK Crime Clustering
  • WP7 Bing Maps Development UK Polygons Clustering Performance
  • WP7 cryptography bouncy castle
  • WP7 Cultures C#
  • WP7 feedback development app store
  • WP7 Javascript web browser
  • WP7 MSBuild
  • WP7 ORM Databases performance
  • WP7 Serialisation
  • WP7 SilverlightSerializer C#
  • WP7 sqlite performance development
  • WP7 WP7Contrib Bing Maps Development
  • WP7 WP7Contrib Bing Maps Polygon Development
  • WP7 WP7Contrib CodePlex
  • WP7 WP7Contrib CodePlex Bing Maps Development
  • WP7 WP7Contrib CodePlex ObservableCollection
  • WP7 WP7Contrib ILMerge .Net
  • WP7 WP7Contrib Phone Maps
  • WP7 WP7Contrib SilverlightSerializer C#
  • WP7Contrib
  • WP7Contrib Bing Maps WP7
  • WP7Contrib WP7 Geo-Location development C#
  • WP7Contrib WP7 HTTP Compression
  • WP7Contrib WP7 Url Development Rx
  • WP7Dev
  • WPF
  • WPF Cultures
  • WuApi
  • XAML

Blog Archive

  • ►  2013 (16)
    • ►  November (5)
    • ►  September (3)
    • ►  August (1)
    • ►  July (1)
    • ►  June (3)
    • ►  May (2)
    • ►  January (1)
  • ►  2012 (44)
    • ►  November (2)
    • ►  October (8)
    • ►  September (5)
    • ►  August (2)
    • ►  July (4)
    • ►  June (3)
    • ►  May (1)
    • ►  April (2)
    • ►  March (13)
    • ►  February (4)
  • ▼  2011 (52)
    • ►  December (3)
    • ►  November (5)
    • ►  October (7)
    • ►  September (7)
    • ►  August (11)
    • ►  July (4)
    • ►  May (2)
    • ►  April (1)
    • ▼  March (5)
      • Calling 'HtmlPage.Window.Invoke' from a background...
      • Considerations when building a caching mechanism f...
      • WP7Contrib: RESTful communications using HttpWebRe...
      • WP7Contrib: Why we use SilverlightSerializer inste...
      • WP7Contrib: Trickling data to a bound collection
    • ►  February (3)
    • ►  January (4)
  • ►  2010 (1)
    • ►  August (1)
  • ►  2009 (32)
    • ►  December (3)
    • ►  November (7)
    • ►  October (6)
    • ►  September (11)
    • ►  April (1)
    • ►  March (4)
Powered by Blogger.

About Me

Unknown
View my complete profile