i have json file populated strings data in documents directory. in user interface of application there uibutton. on button press, new string appends json file.
now looking ios service helps me send these strings (from json file) server using swift. , service should totally independent of code. point when press uibutton, first step string saved json file service should take string , send server if internet available.
when string sent successfully, should removed json file. service should track after every 30 seconds if there string saved json file, send server.
i googled , found background fetch triggers performfetchwithcompletionhandler
function automatically , cannot know when ios triggers it. want trigger kind of service self after every 30 seconds.
review background execution portion of apple's app programming guide ios.
uiapplication
provides interface start , end background tasks uibackgroundtaskidentifier
.
in top level of appdelegate
, create class-level task identifier:
var backgroundtask = uibackgroundtaskinvalid
now, create task operation wish complete, , implement error case task did not complete before expired:
backgroundtask = application.beginbackgroundtaskwithname("mybackgroundtask") { // expirationhandler called when task expired // cleanup task here, remove objects memory, etc application.endbackgroundtask(self.backgroundtask) self.backgroundtask = uibackgroundtaskinvalid } // implement operation of task background task dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0)) { // begin upload , clean json // nsurlsession, alamofire, etc // on completion, end task application.endbackgroundtask(self.backgroundtask) self.backgroundtask = uibackgroundtaskinvalid }
Comments
Post a Comment