ios - Swift - Issue with variable scope with parse.com -


i'm struggling problem since 2 days swift calling parse.com class.

  1. i have variable declared @ root of uiviewcontroller class

    var myvariable: string?

  2. i created function call database , return value

    func retrievedata() {

        // create query     var querytest:pfquery = pfquery(classname: "myclassename")      // clause     querytest.wherekey("columnname", equalto: "sometext")      querytest.getfirstobjectinbackgroundwithblock { (myparseobject:pfobject!, errreur: nserror!) -> void in          self.myvariable = myparseobject["nameofcolumnofcontainerthevaluetobereturned"] as? string          println("valeurtest dans la bd = \(self.myvariable)")     }  } 

so far println inside function returns value! great

the problem starts when call function inside viewdidload. want change value of myvariable calling retrievedata() function. however, variable value not change!

  override func viewdidload() {             super.viewdidload()              self.retrievedata()              // myvariable value should change              println("my new value =\(self.myvariable)")          } 

thanks help!

getfirstobjectinbackgroundwithblock asynchronous (i.e. executes on background thread) have wait complete before accessing value. attempting print after calling getfirstobjectinbackgroundwithblock's method, you're attempting print variable hasn't been retrieved yet since getfirstobjectinbackgroundwithblock hasn't had enough time fetch data.

in first block of code, self.myvariable able print since it's executed within block , after fetch has completed.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -