ios - Parse Query Results Not Appending to New Array -


i'm using swift , i'm trying append parse query's results array, however, array shows appended value when it's printed within query's loop. when try printing array outside of loop before method's return, empty array. how can array appended value show outside of loop?

class task {         func all() -> array<string> {         var taskarray = array<string>()         var query = pfquery(classname:"task")         var currentuser = pfuser.currentuser() // nil         let userid = currentuser?.objectid         query.wherekey("userid", equalto: currentuser!)         query.findobjectsinbackgroundwithblock {             (objects: [anyobject]?, error: nserror?) -> void in              if error == nil {                 // find succeeded.                 println("successfully retrieved \(objects!.count) tasks.")                  // found objects                 if let objects = objects as? [pfobject] {                     object in objects { //                        println(object.objectid)                         let title = object["title"]! as! string                         taskarray.append(title)                         println("array: \(taskarray)")                     }                 }             } else {                 // log details of failure                 println("error: \(error!) \(error!.userinfo!)")             }         }         println("outside query array: \(taskarray)")           return taskarray     } } 

it looks outside query being called before query or along lines of that. debug console:

enter image description here

as said, println("outside query array: \(taskarray)") called before println("array: \(taskarray)") , there simple explanation this.

findobjectsinbackgroundwithblock asynchronous method, means starts thread , works on one. means moment call findobjectsinbackgroundwithblock, continues directly println("outside query array: \(taskarray)").


now, possible download stuff parse synchronously (so not skip directly println("outside query array: \(taskarray)")), not recommend this.

instead recommend implement closure called when download finished. similar following (ps, it's been written in swift 2):

// //  viewcontroller.swift //  parsefun // //  created stefan veis pennerup on 21/09/15. //  copyright (c) 2015 kumuluzz. rights reserved. //  import uikit  class viewcontroller: uiviewcontroller {      // mark: - lifecycle methods      override func viewdidload() {         super.viewdidload()          { (tasks) -> void in             print("the downloaded tasks are: \(tasks)")         }     }      // mark: - download methods      func all(completionhandler: ([string])->void) {         // creates query         var taskarray = array<string>()         let query = pfquery(classname:"task")         let currentuser = pfuser.currentuser() // nil         query.wherekey("userid", equalto: currentuser!)          // starts download         query.findobjectsinbackgroundwithblock {             (objects: [anyobject]?, error: nserror?) -> void in              if error == nil {                 // find succeeded.                 print("successfully retrieved \(objects!.count) tasks.")                  // found objects                 if let objects = objects as? [pfobject] {                     object in objects {                         let title = object["title"]! as! string                         taskarray.append(title)                         print("array: \(taskarray)")                     }                 }                  completionhandler(taskarray)              } else {                 // log details of failure                 print("error: \(error!) \(error!.userinfo)")             }         }     } } 

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 -