-2

Here is the code:

var array:[String] = []
var second:[String] = [" "]

 override func viewDidLoad() {
    super.viewDidLoad()
    search.delegate = self //HELP

    let query = PFQuery(className: "Classes")
    query.whereKey("name", notEqualTo: " ")
    query.findObjectsInBackground(block: { (objects, error) in
        if error != nil{
           print("cannot retrieve classes")
        } else {
            for object in objects!{
                self.array.append(object["name"] as! String)
                self.second = self.array
                self.classList.reloadData()
                print(self.array)
            }
        }
    })
     print(second)
    print(array)


}

After printing self.array, there are elements in the array, but if I just print(array) at the end, the array is still empty. Why is this? Can someone help clarify?

1 Answers1

3

You are printing array on main queue. The objects are being added on background queue. Actually the print(array) gets called even before you have added any object via background queue. Thats why its prints empty array.

Mohammad Sadiq
  • 5,070
  • 28
  • 29