-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
  • 4
    What do you think that findObjectsInBackground does? Commented Jul 22, 2017 at 20:56

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.