Sunday, 15 June 2014

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


<'m Html>

I am struggling with a problem parse.com class call with SWIFT since 2 days.

  1. I have been declared a variable which is the root of Uaiviaiuaiuaiutiai controller class

    var myVariable: string?

  2. I return a function built database and a value to call

    func retrieve data () {

      // query create var queryTest: PFQuery = PFQuery (className: "myClasseName") // section where QueryTest.whereKey ( "columnName", equivalent to "some text") queryTest.getFirstObjectInBackgroundWithBlock {(myParseObject: PFObject!, errreur: NSError !) - & gt; Zero in self MyVariable = myParseObject ["nameOfColumnOfContainerTheValueToBeReturned"] as? String println ( "valeurTest dans la BD = \ (self.myVariable)")}}  

So far Println within function gives me the value! Great

The problem begins when I call in my work and see DidDod. By calling the recovery data () function, I want to change the value of my variable However, there is no change in variable value!

  override func viewDidLoad () {super.viewDidLoad () self.retrieveData () // myVariable value to println ( "My new value = \" (Self.myVariable) ")}  

Thank you for your help!

getFirstObjectInBackgroundWithBlock is asynchronous (i.e. it executes on the background thread) so that you have to wait before reaching the value to complete it. Attempting to print immediately after calling the getFirstObjectInBackgroundWithBlock method By doing, you get a variable Trying to print that has not been recovered yet because there is not enough time in getFirstObjectInBackgroundWithBlock .

In your first block of code, self .myVariable is able to print successfully because it is executed in block and after this fetch is complete.


No comments:

Post a Comment