isa56k
isa56k

Reputation: 145

Parse.com Swift getFirstObjectInBackground

Can any one give an example of how use getFirstObjectInBackground and get the first Object returned into a PFObject in Swift?

    let weatherObject:PFObject = query.getFirstObjectInBackground() as! PFObject

Gives a warning Cast from 'BFTask' to unrelated type 'PFObject' always fails is the warning / error I get in xcode.

TIA.

Upvotes: 0

Views: 759

Answers (1)

danh
danh

Reputation: 62676

That variety of get returns a BFTask from the bolts framework, which is kind of like a JS promise for the iOS sdks. Your code casts the return from getFirstObjectInBackground to a PFObject, which it isn't, as if the method synchronously returns the object you're trying to fetch, which it doesn't.

The fix is either to treat the BFTask return value like a BFTask and assign it a completion block, or -- easier I think -- use the block variety of the get method (e.g. from the iOS guide):

query.getFirstObjectInBackgroundWithBlock {
  (object: PFObject?, error: NSError?) -> Void in
  if error != nil || object == nil {
    println("The getFirstObject request failed.")
  } else {
    // The find succeeded.
    println("Successfully retrieved the object.")
  }
}

If your situation really calls for handling the bolts framework directly, a decent doc for it can be found on its github page.

Upvotes: 1

Related Questions