Array retuning a blank array outside of PFQuery with Parse.

前端 未结 3 497
难免孤独
难免孤独 2021-01-27 22:42

Array returning a blank array when outside of the PFQuery. For some reason, the items are not being passed to the array when compiled.

class DriverViewController         


        
3条回答
  •  难免孤独
    2021-01-27 23:21

    This a very common misunderstanding relating to threading, the issue is what order events run:

    // Runs 1st
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in
        // Runs 3rd
    }
    // Runs 2nd
    println(placesArr)
    

    The execution of the program doesn't halt when you call findObjectsInBackground, it finds objects: inBackground which means the heavy lifting of a network request is dispatched to a different queue so that the user can still interact with the screen. A simple way to do this would be to do:

    var placesArray: [Place] = [] {
        didSet {
            // Do any execution that needs to wait for places array here.
        }
    }
    

    You can also trigger subsequent actions within the parse response block, I just personally find executing behavior dependent on a property update being executed in didSet to be a nice way to control flow.

提交回复
热议问题