Joel Fernandes
Joel Fernandes

Reputation: 4856

Swift/iOS - Syncing Offiline Core Data with Server when Device is Back Online

I'm trying to understand how I can send my offline data to the server when the device is online.

Can this be achieved without having the user to open the app, and send the data immediately when the device is connected to the Internet in background?

Upvotes: 1

Views: 1515

Answers (1)

CouchDeveloper
CouchDeveloper

Reputation: 19106

One viable approach would utilise file uploads with a "background session".

What it requires is a file that represents your Core Data objects. When you have that, create a NSURLSession object with a background configuration using static function backgroundSessionConfigurationWithIdentifier(_:).

When you schedule a task with the background session, it will be executed by the system in the background, even when your app is terminated. The system only tries to download the file when it has a connection.

For more information see Using NSURLSession and Handling iOS Background Activity.

There are a few configuration options (NSURLSessionConfiguration) which may seem important in your case, too:

discretionary and sessionSendsLaunchEvents

Upvotes: 1

Related Questions