Getting parse.com data in Swift

I was able to successfully save Parse via Swift, but I'm having problems getting the data (and all the fetching tutorials seem to be for Obj-C).

Here's my code (with id edited).

Parse.setApplicationId("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", clientKey: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")


    var query = PFQuery(className: "EventObject")
    query.getObjectInBackgroundWithId(objectId: String!) {
        (event: PFObject!, error: NSError!) -> Void in

        if error == nil {
            println(event)

        } else {

        println(error)

        }

    }

      

I have 4 entries in this class right now, but if I want to pull data for all of them, how do I get an object using an ID, if I'm not sure what the IDs are? I am guessing that I can access them sequentially as an array, but I am not quite sure how to do this, and I am confused as the only command I know to get it seems to require knowing the ID.

Thanks for any help!

+3


source to share


4 answers


The official parsing documentation explains how to make queries - there is sample code in swift.

In your case, you should use findObjectsInBackgroundWithBlock

:



var query = PFQuery(className:"EventObject")
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
  if error == nil {
    for object in objects {
        // Do something
    }
  } else {
      println(error)
  }
}

      

which, if successful, provides the closure with an array of objects matching the request - since there is no filter in the request, it just returns all records.

+4


source


Swift 2.1 update



func fetchFromParse() {
    let query = PFQuery(className:"Your_Class_Name")
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if error == nil {
            for object in objects! {
                // Do something
            }
        } else {
            print(error)
        }
    }


}

      

+1


source


Here is the code for retrieving objects in Swift3.0 .

let query = PFQuery(className: "Your_Class_Name")
    query.findObjectsInBackground { (objects, error) in
        if error == nil {

        }
        else {

        }
    }

      

0


source


Extract data from parsing: fast

 let adventureObject = PFQuery(className: "ClassName")
    adventureObject.addAscendingOrder("objectId")
    var objectID: String = String()
    adventureObject.findObjectsInBackground(block: { (Success, error) in
    })

      

0


source







All Articles