Gracefully handle background task when app tombsto

2019-09-09 09:30发布

问题:

I'm using the async ctp and have a task that runs in the background. It works great but throws an exception when it's running and the app is tombstoned. The exception is related to the database context being null.

The docs say:

When an application is tombstoned, the underlying database connections are closed. To return to its previous state after tombstoning, an application needs to redo any queries it had performed before it was tombstoned.

I've tried using a cancellationtokesource but this doesn't seem to work as the connection is already closed. Any idea how to handle something like this?

回答1:

We also had a number of problems with this, but the general architecture of a mobile app tends to be service oriented so most people will not have to worry about database connections. Handling tombstoning is error prone in general - this area was one of the prolific bug generators in my project.

It is OK (not great) as a user experience to return to the start screen after waking up from tombstoning and not to the last screen. Otherwise you could cache the data you already have - using the IsolatedStorage and try to redo the screen.