AlamoFire Upload in Background Session

Viewed 2634

im trying to upload files to my server while the app is in background, i have an NSOperation set up as following:

class uploadOperation: NSOperation {

    weak var request: Alamofire.Request?

    override func main() {

        let task = backgroundTaskManager.sharedManager.backgroundTask

        request = task.upload(.POST, "http://foo.com", headers: nil, file: fileURL).responseJSON { response in
        }
    }

    override func cancel() {
        request?.cancel()
        super.cancel()
    }
}

i set the background session configuration.

class backgroundTaskManager: NSObject {

    class var sharedManager:backgroundTaskManager {
        struct Static {
            static let instance:backgroundTaskManager = backgroundTaskManager()
        }
        return Static.instance
    }

    lazy var backgroundTask: Alamofire.Manager = {
        let identifier = "com.MyAppName.background"
        return Alamofire.Manager(configuration: NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(identifier))
    }()
}

in app delegate

func application(application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: () -> Void) {

    backgroundTaskManager.sharedManager.backgroundTask.backgroundCompletionHandler = completionHandler
}

this is my setup for "Required background modes": Required background modes

* i am using Alamofire 3.2.1 *

when i go to background the app upload 1 files and then stops, what did i do wrong ?

0 Answers
Related