ErkanA
ErkanA

Reputation: 179

findObjectsInBackgroundWithBlock not entering the "Block"

My code is something like following, and what I don't understood is why the code after Void in never runs? I did tried to debug but it appears Block never gets executed.

By the way, query will return empty.

let query = PFQuery(className: "LastId")
query.whereKey("UserId", equalTo: opUserIdList[i])
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
    if let objects = objects {
        for object in objects {
            object.setValue(self.opLastIdChangedToList[i], forKey: "lastId")
            object.saveInBackground()
        }
    } else {
        let newLine = PFObject(className: "LastId")
        newLine["lastId"] = self.opLastIdChangedToList[i]
        newLine["userIdself."] = self.opUserIdList[i]
        newLine.saveInBackground()
    }
})
//rest of the code

Upvotes: 0

Views: 154

Answers (1)

Alexey Pichukov
Alexey Pichukov

Reputation: 3405

This is because the block of code after Void in is the closure. To say in simple that is a pointer to a function containing the code (code block) that is executed later, after the completion of the parent function in which it was called.

Try reading the documentation to better understand how to work with it: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-ID94

Upvotes: 1

Related Questions