0

I am saving user entered data to MySQL Database using urlSession’s dataTask when network is available. I am saving user entered data in local SQLite when no network available.

How to schedule background task for syncing it when network is available even when app is not running?

James Z
  • 12,209
  • 10
  • 24
  • 44
Jaya N
  • 1
  • 2

1 Answers1

0

You cannot check the connectivity status and make the URL Request if your app is not running. An alternate solution may be to make the request once the app is opened by the user the next time. At that time you will get a call back in func applicationDidFinishLaunching in the AppDelegate. At this point you check if you have data which you need to sync to the MySql Data base and make the request.

Edit

As you mentioned in the comment. In iOS you cannot detect the network when the app is closed SO Post. It is possible to do so using backgroundMode. Refer to this SO Post

But as you pointed. There is one way of achieving it by geo fencing. An iOS Application wakes up when it enters a region you set a geo fence to. So at that you can make the API request to wake the app. SO Post 2

Edit2

You can wake up the app using silent notification.

Hope this helps. Please let me know if you need any clarifications. Welcome to stackoverflow. Please mark the answer if it you solves the issue you are facing.

Community
  • 1
  • 1
Md. Ibrahim Hassan
  • 5,359
  • 1
  • 25
  • 45