3

I have developed an application. My application takes feedback from users. When network is not available, then that data is saved in local db. But, once the network is available, it will sync automatically with the central database.

But, I have some problem here. If I save database & send my application to background, once the network is available, it has to automatically do this syncing with the central database. How to do that?

I am using Reachability class to check network availability.

Bharath
  • 3,001
  • 6
  • 32
  • 65

3 Answers3

1

You cannot do it on iPhone. Your app ceases to exist in a few seconds (once the app moves to the background).

I believe its 5 seconds for all apps, 10 mins for some apps that have requested for more background time.

PS: Unless, you mark your app as a navigation or a music app, which can stay on in the background, theoretically, forever. But I doubt if a feedback app can get approved on the appstore with such permissions.

Nitin Alabur
  • 5,812
  • 1
  • 34
  • 52
1

At the present time there is no way to have your app "wake up" when the network becomes available. If the user quit your app without a network connection, you cannot do anything until they voluntarily open your app.

However, you can prompt them to do so using UILocalNotification. If your app is being quit and you have some data waiting to be uploaded, you can schedule a notification to fire in 4 hours (or whatever amount of time makes sense).

If the user opens the app before the notification time and you are able to upload the data, you can cancel the notification and no one will ever know it was scheduled.

If the user does not open the app, the notification will appear, and say something like, "You have data on your phone that you have not uploaded in a while. Connect to the Internet and launch MyAwesomeApp to sync your data."

benzado
  • 82,288
  • 22
  • 110
  • 138
  • @benzado.. If I use UILocalNotification, every time the network status changes, these notifications get fired & application becomes a disturbance with lot of local notifications. Anyways, I liked this idea. Incase If that particular user ignores the notification, then this notification will be fired again and again because, Reachability class `kReachabilityChangedNotification ` is fired many times when reachability changes. – Bharath Aug 01 '12 at 09:48
  • 1
    @taruninima You misunderstand. You only schedule ONE notification, when your app is quitting; when your app is open, you cancel it. The user won't see a notification unless they haven't opened your app in a day or so. – benzado Aug 01 '12 at 19:28
0

Keep an additional column in your saved database which marks successful uploading of your data to your server. Set this when the data is written, but not yet uploaded. When it's successfully uploaded, clear the value. You can check this value when your app comes to the foreground, and have it upload any data that hasn't had this column cleared. While your app is running, you can set a timer for an appropriate interval to recheck reachability and if successful, attempt an upload. Only clear your flag when the data is successfully written, and make sure your server doesn't try to process a partial upload (think of someone trying to do this on a subway or train, moving in and out of connectivity).

Owen Hartnett
  • 5,925
  • 2
  • 19
  • 35
  • @Owen.. I know how to handle whether data is synced with central db or not. But My question is not this. Please refer to the question again. – Bharath Aug 01 '12 at 09:42
  • If there is no time constraint as to when the data has to arrive, you can really just wait until you become active again. Of course, there is no guarantee of delivery if the user never reactivates your app. – Owen Hartnett Aug 01 '12 at 13:53