Trevor Yokum
Trevor Yokum

Reputation: 160

Backgrounding in iOS with Xamarin.iOS (C#): Which method should I use?

I'm having some trouble with backgrounding on iOS. First, let me give a brief explainer:

I work for a work management company that can receive and distribute tickets for locating utilities. Users can download tickets from our server, work on them, and send their responses back to our server. The downloaded tickets go into a local SQLite database and the responses that the user makes are stored in another local SQLite database. My coworker wrote the Android app in Xamarin with C#, and his intelligent process to download tickets and upload responses is as follows:

  1. Download tickets from our server to a local SQLite database, "tickets"
  2. Check the local "responses" SQLite database to see if there are responses made
  3. If there are responses made, send them to our server
  4. Wait 5 minutes
  5. Repeat

This allows a user to initially download a batch of tickets and go work on them. If this process were to run while the user did not have internet, nothing would happen, and the timer would reset. This process would allow a user to make responses to tickets without internet, and would upload responses/download new tickets whenever they had internet.

My issue is that, on iOS, backgrounding is much more restrictive than on Android. There is background fetch and then there are push notifications. With background fetch, the system "calculates the best opportunity" at which to run a PerformFetch method and does not give me the rigid 5 minute timer that is necessary. With push notifications, if the user does not have internet then they will never receive the push notification.

I would really love to be able to simply set a 5 minute timer as it was described above and have the process run, checking if there are any new tickets and sending any new responses if there are any present. But I can't do that due to the restrictive nature of backgrounding in iOS.

Any suggestions? They'd be more than appreciated.

Thanks.

Upvotes: 1

Views: 78

Answers (1)

PlusInfosys
PlusInfosys

Reputation: 3446

Its true that you cant create background service in iOS App like Android. But after reading your requirement it seems like you don't really need background Service for all task- You need it only when user has updated data offline and closed app - never open again when internet connection is available again. But that is restriction on iOS, you can't achieve this anyway in iOS.

For other task, You can fetch data asynchronously from server when your app is active.

You can simply create timer of 5 minutes - which will run only if your app is active. and on that time, you can execute code to fetch new tickets and send response to server in Async mode. Check here for more detail about how to do this in Xamarin.

This process as its async, will not disturb regular UI activity. Users can check the database for new ticket, update response in database etc on timer whenever it has internet connectivity.

Upvotes: 1

Related Questions