Suspending a WinRT app that uses Okra

Aug 22, 2012 at 3:16 AM

Andy,

Any thoughts on saving a PagedDataListSource<T> when the app is suspending? Normally I'd use Json.Net to serialize the collection to disk and deserialize it on resume.

Coordinator
Aug 22, 2012 at 1:50 PM
Edited Aug 22, 2012 at 1:52 PM

You mention saving the contents of a PagedDataListSource<T> to disk on application suspesion. Whilst the Okra framework allows you to hook into application suspension events (via the ILifetimeManager and ILifetimeAware interfaces) you should remember that you only have a limited period of time to save data upon suspension before your application is forcibly terminated. Hence the guidelines recomend that data is saved in the background whilst the app is running if possible rather than waiting for the suspension event.

My thoughts on this would be to save each page of data as it is retrieved - either in a seperate file for each page, or a single combined file that is updated each time. In your FetchPageAsync(...) method you could continue to use Json.Net (or any other serialization technique) to save the resulting DataListPageResult<T> object to disk, then on subsequent calls you could retrieve the stored value if available, otherwise continue as normal.

In pseudo-code this would be,

protected override async Task<DataListPageResult<MyItem>> FetchPageAsync(int pageNumber)
{
    DataListPageResult<MyItem> cachedPage = await GetPageFromDisk(pageNumber);

    if (cachedPage != null)
        return cachedPage;

    DataListPageResult<MyItem> page = await GetPageFromDataSource(pageNumber);

    SavePageToDisk(pageNumber, page);

    return page;
}

Note that the SavePageToDisk(...) method should be marked as 'async' so that it would not block the thread until the data was saved.

How does this sound for your use-case? Let me know and I can adapt as required...

Andy

Aug 30, 2012 at 2:50 AM

I'm going to avoid this for the moment, I noticed quite a few apps don't handle it in the store. I'm hoping I wont fail certification. It's a difficult problem to solve. I agree with you about saving as you go though. I'd love to see a full app example that handles this.

I've got a complex app with multiple collections and many views. I keep track of the current item for each one and quite a bit of other data that is tough to restore in the correct order. The service I'm calling doesn't support page numbers so each request requires me to pass the last id of an item to get the next batch. I'd have to keep some sort of manifest of what was stored and loop through restoring pages for each specific collection before I restored the current item for each.

Coordinator
Aug 30, 2012 at 11:02 AM

This definately a tricky problem to solve - caching in general is hard to get right. Have you seen the AgFx caching framework for Silverlight? I think there may even be a Win8 port too. Might be worth a look for an alternative approach.