ios - How to implement background fetch correctly in ios swift -


i have situation using background fetch call data sync process, sync function heavy task, executed in background thread.

here code,

func application(application: uiapplication, performfetchwithcompletionhandler completionhandler: (uibackgroundfetchresult) -> void) {     print("background fetch")     utilities.synccompleted = false // declared :> static var synccompleted:bool = false     backgroundsync().startsync() // heavy background task, , iam updating [utilities.synccompleted = true) on thread completion     while utilities.synccompleted == false {         nsthread.sleepfortimeinterval(1) // sleep sometime     }     if utilities.synccompleted{         completionhandler(uibackgroundfetchresult.newdata)     }else {         completionhandler(uibackgroundfetchresult.nodata)     } } 

now have questions :

  1. as background fetch of 30 sec, if task not completed in 30 sec happens, because wont able set completionhandler .nodata or .failure

  2. is there default completionhandler value set (like .nodata) if developer not specify in 30 sec.

  3. is there other better way this.

thanks in advance

actually, if starting backgroundtask, not limited 30s, 10 minutes or whatever current limit those.

background fetch full-on starting of app, including access main thread, ui changes etc. equivalent starting app minus actual on-screen display. background task more limited in can allowed take longer.

thus in case not care returning proper value of nodata or newdata. start sync background task , call completionhandler(uibackgroundfetchresult.newdata).

if want fair system possible, can check application.backgroundtimeremaining , schedule dispatch_after before expiring. if task finished before it, send newdata/nodata received, if takes longer, dispatch_after block send newdata , done it.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -