Tomasz Cielecki wrote
> 
> I guess this should work, but mind that your app might run out of
> memory if you cache a lot of views in the memory. Also keep in mind
> that while the application is in suspended mode iOS may purge the
> memory of the application and you will have to reload the views
> anyways.
> 

Ah, well aware of that! I simplified it a little bit, in reality the cache
Get method works something like this

public static T Get<T>() where T : class, new()
{
    string key = typeof(T).ToString();
    T data = null;

    if (CachedViews.ContainsKey(key))
        data = CachedViews[key] as T;
    
    if (data == null)  // not in cache or eaten by a grue
   {
       data = new T();
       CachedViews[key] = data;
   }

   return data;
}

so...
var controller = ViewControllerCache.Get<MyFantasticView>();

...would give me MyFantasticView if it's in the cache, or a new instance if
it's not. And that made me feel very smart for a few minutes. It all starts
to fall apart if I save a viewcontroller in the cache and then push it onto
the navigator later, deeper down the navigation hierarchy. 


Tomasz Cielecki wrote
> 
> The normal approach is as you said yourself to create the view when
> needed and dispose of it when it is not needed anymore. I am not sure
> what the time consuming part of your views are, but usually my views
> load instantly and the loading of the data to be put into the views is
> what consumes most of the time.
> 
> So maybe a better approach to this could be to save the state of the
> data in the view rather than the view itself. If it is really the view
> that is taking that much time to load, then try to see if it can be
> optimized.
> 

Hmm, that's an idea worth exploring. For some reason I can't quite get the
profiling tools to work so I'm stuck with adding stopwatches here and there,
making it somewhat tricky to figure out what's really slowing down the
transitions. And it's not like it's unbearable, but having just a single
second delay between views really make the whole experience so much worse.

Xamarin people: Please give us a guide on how to best optimize our apps ;-)



On Thu, Jun 21, 2012 at 1:11 PM, victoria &lt;iminurbase@&gt; wrote:
> Dear list,
>
> Short version: What's the correct way to load a UIViewController when the
> application starts, and then restore it (much) later?
>
> *
>
> I found creating UIViewControllers with somewhat complex Views to be a
> fairly expensive operation, so instead of just creating and pushing them
> onto the Navigator when needed, I had the idea to load all heavy views
> into
> a simple object cache when the app starts (with a nice loading screen),
> and
> then push/pop them at will while keeping them in memory. It works
> something
> like this:
>
> 1 AppStartUp
> |--- cache["finalView"] = new ComplexViewController();
> |--- push Menu A onto the navigator
> |
> 2 Menu A Controller
> | --- user makes some kind of selection
> | --- (cache["finalView"] as
> ComplexViewController).UpdateViewWithSelectionFromMenuA()
> |---  push Menu B onto the navigator
> |
> 3 Menu B Controller
> | --- user makes some kind of selection
> | --- (cache["finalView"] as
> ComplexViewController).UpdateViewWithSelectionFromMenuB()
> |---  push cache["finalView"] onto the navigator
> |
> 4 ComplexViewController
> | --- OMG Ponies!
>
>
> This method at first seemed to work great, and made the transition between
> Menu B and ComplexViewController *a lot* smoother. However, things started
> to fall apart recently with stuff going missing from the views in the
> cache.
> So, I started adding lots of SetNeedsDisplay() in their ViewWillAppear
> methods, and this somewhat worked - except for UITables. And some custom
> controls. I'm fairly certain I've gone down the wrong path here, so are
> there any best practices for doing what I'm trying to accomplish?
>
> Any help or hints would be much appreciated!
>
> / V
>
> --
> View this message in context:
> http://monotouch.2284126.n4.nabble.com/Correct-way-to-cache-and-restore-a-UIViewController-tp4655514.html
> Sent from the MonoTouch mailing list archive at Nabble.com.
> _______________________________________________
> MonoTouch mailing list
> [email protected]
> http://lists.ximian.com/mailman/listinfo/monotouch



-- 
Med Venlig Hilsen / With Best Regards
Tomasz Cielecki
http://ostebaronen.dk
_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch



--
View this message in context: 
http://monotouch.2284126.n4.nabble.com/Correct-way-to-cache-and-restore-a-UIViewController-tp4655514p4655521.html
Sent from the MonoTouch mailing list archive at Nabble.com.
_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch

Reply via email to