xcode - How to wait for a background function to finish before calling another one in swift? -


i try explain briefly situation is. building quiz app , wanted work using internet, work while when user disconnected. older code using made synchronous queries, taking more time expected. decided reformulate it. situation projected following:

  • when user selects subject app synchronously 1 question each subsubject in order ready user click , faster.

  • after getting first question, app 4 (or how needed complete 5) each of subsubjects asynchronously, while user occupied answering first question presented him.

  • finally, app save objects in local datastore, user can answer 5 questions each subsubject when not connected.

here code:

func getquestionsremotelyandsave (subsubject:string?, subject:string?, arrayofsubsubjects:[string]?) {     self.getfromuserdefaults()     if reachability.isconnectedtonetwork() == true {             if self.needstogetquestions == true {                 let user = pfuser.currentuser()                 var query = pfquery(classname: "questions")                 query.wherekey("answeredby", equalto: user)                 query.wherekey("grade", equalto: user["grade"])                  if subsubject != nil && subject == nil {                     query.wherekey("subsubject", equalto: subsubject)                     query.limit = 2                     let array = query.findobjects()                     item in array {                         let object = item pfobject                         var questiontopin = pfobject(classname: "questions")                         questiontopin["question"] = object["question"] string                         questiontopin["rightanswer"] = object ["rightanswer"] string                         questiontopin["wronganswer1"] = object ["wronganswer1"] string                         questiontopin["wronganswer2"] = object ["wronganswer2"] string                         questiontopin["wronganswer3"] = object ["wronganswer3"] string                         questiontopin["grade"] = object["grade"] string                         questiontopin["subject"] = object ["subject"] string                         questiontopin["subsubject"] = object ["subsubject"] string                         questiontopin["feedback"] = object ["feedback"] as? string                         questiontopin["shortfeedback1"] = object ["shortfeedback1"] as? string                         questiontopin["shortfeedback2"] = object ["shortfeedback2"] as? string                         questiontopin["shortfeedback3"] = object ["shortfeedback3"] as? string                         questiontopin.pin()                     }                     query.limit = 3                     query.findobjectsinbackgroundwithblock({ (objects:[anyobject]!, error:nserror!) -> void in                         if error == nil {                             let questions = objects [pfobject]                             var questionstopin = [pfobject]()                             object in questions {                                 var questiontopin = pfobject(classname: "questions")                                 questiontopin["question"] = object["question"] string                                 questiontopin["rightanswer"] = object ["rightanswer"] string                                 questiontopin["wronganswer1"] = object ["wronganswer1"] string                                 questiontopin["wronganswer2"] = object ["wronganswer2"] string                                 questiontopin["wronganswer3"] = object ["wronganswer3"] string                                 questiontopin["grade"] = object["grade"] string                                 questiontopin["subject"] = object ["subject"] string                                 questiontopin["subsubject"] = object ["subsubject"] string                                 questiontopin["feedback"] = object ["feedback"] as? string                                 questiontopin["shortfeedback1"] = object ["shortfeedback1"] as? string                                 questiontopin["shortfeedback2"] = object ["shortfeedback2"] as? string                                 questiontopin["shortfeedback3"] = object ["shortfeedback3"] as? string                                 questiontopin.pininbackground()                             }                         }                     })                 }                  if subsubject == nil && subject != nil {                     var firsttime = true                     var semaphore = dispatch_semaphore_create(0)                     query.limit = 1                     item in arrayofsubsubjects! {                         if item != "todas matérias" {                             var query3 = pfquery(classname: "questions")                             query3.fromlocaldatastore()                             query3.wherekey("subsubject", equalto: item)                             let count = query3.countobjects()                             if count == 0 {                                 query.wherekey("subsubject", equalto: item)                                  let array:nsarray = query.findobjects()                                 item in array {                                     let object = item pfobject                                     var questiontopin = pfobject(classname: "questions")                                     questiontopin["question"] = object["question"] string                                     questiontopin["rightanswer"] = object ["rightanswer"] string                                     questiontopin["wronganswer1"] = object ["wronganswer1"] string                                     questiontopin["wronganswer2"] = object ["wronganswer2"] string                                     questiontopin["wronganswer3"] = object ["wronganswer3"] string                                     questiontopin["grade"] = object["grade"] string                                     questiontopin["subject"] = object ["subject"] string                                     questiontopin["subsubject"] = object ["subsubject"] string                                     questiontopin["feedback"] = object ["feedback"] as? string                                     questiontopin["shortfeedback1"] = object ["shortfeedback1"] as? string                                     questiontopin["shortfeedback2"] = object ["shortfeedback2"] as? string                                     questiontopin["shortfeedback3"] = object ["shortfeedback3"] as? string                                     questiontopin.pin()                                 }                              }                              if count > 0 {                                 var limit = 5 - count                                 if limit < 0 {                                     limit = 0                                 }                                 query.limit = limit                                 if firsttime == false {                                     dispatch_semaphore_wait(semaphore, dispatch_time_forever)                                 }                                 query.findobjectsinbackgroundwithblock({ (objects:[anyobject]!, error:nserror?) -> void in                                     if error == nil {                                         let questions = objects [pfobject]                                         object in questions {                                             var questiontopin = pfobject(classname: "questions")                                             questiontopin["question"] = object["question"] string                                             questiontopin["rightanswer"] = object ["rightanswer"] string                                             questiontopin["wronganswer1"] = object ["wronganswer1"] string                                             questiontopin["wronganswer2"] = object ["wronganswer2"] string                                             questiontopin["wronganswer3"] = object ["wronganswer3"] string                                             questiontopin["grade"] = object["grade"] string                                             questiontopin["subject"] = object ["subject"] string                                             questiontopin["subsubject"] = object ["subsubject"] string                                             questiontopin["feedback"] = object ["feedback"] as? string                                             questiontopin["shortfeedback1"] = object ["shortfeedback1"] as? string                                             questiontopin["shortfeedback2"] = object ["shortfeedback2"] as? string                                             questiontopin["shortfeedback3"] = object ["shortfeedback3"] as? string                                              questiontopin.pininbackground()                                         }                                         dispatch_semaphore_signal(semaphore)                                         firsttime = false                                     }                                 })                              }                         }                     }                 }              }       }   } 

the problem i'm facing cant request multiple asynchronous functions parse. thought maybe waiting asynchronous function finish before other 1 starts, wanted happen without user having wait. there way of doing it?

thank you.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -