ios - Swift - Issue with variable scope with parse.com -
i'm struggling problem since 2 days swift calling parse.com class.
i have variable declared @ root of uiviewcontroller class
var myvariable: string?
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
Post a Comment