0

i have here a swift 2 with parse.com code

the problem is the code printing // 2 before //1 that make a problem i can not use the array in other methods or other places here is the code

 var myStudent:Student = Student ()

        let query = PFQuery(className: "Students")
                query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error:NSError?) -> Void in

                    var the_array = objects as! AnyObject

                    for i in 0...the_array.count-1
                    {

                myStudent.NameStudent = the_array[i].valueForKey("name") as! String
                self.myStudentsArray.append(myStudent)
                        print(self.myStudentsArray.count) //1

            }
        }
        print(self.myStudentsArray.count) //2
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
bsm-2000
  • 265
  • 2
  • 13
  • This is intended. `findObjectsInBackgroundWithBlock` works asynchronously. Consider to change your design from passive (waiting for a return value) to active (make your changes/updates in the completion block). – vadian Nov 08 '15 at 20:11
  • i know the reason , and using findObjectsInBackgroundWithBlock that so fast for the code my question is there any solution for this code ? – bsm-2000 Nov 08 '15 at 23:44

1 Answers1

0

This is because the findObjectsInBackgroundWithBlock runs asynchronously on a background thread.

A solution is to call any function that needs the accurate .count within the closure, this way you can be sure to get the correct Array.count.

var myStudent:Student = Student ()

    let query = PFQuery(className: "Students")
            query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error:NSError?) -> Void in

                var the_array = objects as! AnyObject

                for i in 0...the_array.count-1
                {

            myStudent.NameStudent = the_array[i].valueForKey("name") as! String
            self.myStudentsArray.append(myStudent)
                    print(self.myStudentsArray.count) //1
          //Call any function that needs the students count here. This would 
         //be the only way you can guarantee you will get the correct count within the closure.

        }
    }
    print(self.myStudentsArray.count) //2

This is a great tutorial about the Grand Central Dispatch (GCD) - RayWenderlich. If you need print 2 to execute last, try to implement the code in the same closure.

You can also refer to Stack answer Synchronous vs Asynchronous. The question is on Objective C, however, the principles are the same to Swift.

Further, if you want to learn about Concurrency in Swift, you can refer to this previous Stack answer How to do multithreading, concurrency or parallelism in iOS Swift

Community
  • 1
  • 1
Tal Zion
  • 6,308
  • 3
  • 50
  • 73
  • i know the reason , and using findObjectsInBackgroundWithBlock that so fast for the code than findObjects only my question is there any solution for this code ? – bsm-2000 Nov 08 '15 at 23:47