Search code examples
iosswiftsynchronizationbackground-fetch

How to run operations in background while an iOS application is in foreground


I have a JSON file populated with strings data in Documents Directory. In user Interface of application there is a UIButton. On button press, a new string appends into the JSON file.

Now I am looking for any iOS Service that helps me to send these strings (from JSON file) to the server using swift. And this service should be totally independent of my code. The point is when I press a UIButton, the first step is a string is saved to the JSON file then service should take this string and send it to server if Internet is available.

When a string sent successfully, it should be removed from the JSON file. This service should track after every 30 seconds if there is any string saved into JSON file, then send it to server.

I Googled and found background fetch but It triggers performFetchWithCompletionHandler function automatically and I cannot know when iOS triggers it. I want to trigger this kind of service my self after every 30 seconds.


Solution

  • Review the Background Execution portion of Apple's App Programming Guide for iOS.

    UIApplication provides an interface to start and end background tasks with UIBackgroundTaskIdentifier.

    In the top level of your AppDelegate, create a class-level task identifier:

    var backgroundTask = UIBackgroundTaskInvalid
    

    Now, create your task with the operation you wish to complete, and implement the error case where your task did not complete before it expired:

    backgroundTask = application.beginBackgroundTaskWithName("MyBackgroundTask") {
        // This expirationHandler is called when your task expired
        // Cleanup the task here, remove objects from memory, etc
    
        application.endBackgroundTask(self.backgroundTask)
        self.backgroundTask = UIBackgroundTaskInvalid
    }
    
    // Implement the operation of your task as background task
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
        // Begin your upload and clean up JSON
        // NSURLSession, AlamoFire, etc
    
        // On completion, end your task
        application.endBackgroundTask(self.backgroundTask)
        self.backgroundTask = UIBackgroundTaskInvalid
    }