3

I've been able to save successfully to Parse via Swift, but am having trouble retrieving data (and all of the tutorials on retrieving seem to be for Obj-C).

Here's my code (with Id's redacted).

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 records in this class right now, but if I want to pull the data for all of them, how do I get the Object using the ID if I'm not sure what the IDs are? I'm assuming I can access them sequentially as an array, but I'm not quite clear how to do that, and am confused, as the only command I know to retrieve appears to require knowing the ID.

Thanks for any help!

4 Answers 4

4

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

In your case you have to 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 to the closure an array of objects matching the query - since there's no filter set in the query, it just returns all records.

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

2 Comments

I got this error message, /Users/MNurdin/Documents/iOS/xxxxx/Controllers/Profile/TableViewController.swift:40:15: Cannot invoke 'findObjectsInBackgroundWithBlock' with an argument list of type '(([AnyObject]!, NSError!) -> Void)'
But how we can retrieve all objects. I mean all rows from the table?
1

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)
        }
    }


}

Comments

0

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

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

        }
        else {

        }
    }

Comments

0

Retrive Data from parse: swift 3

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

1 Comment

Please edit your answer to include some explanation. Code-only answers do very little to educate future SO readers. Your answer is in the moderation queue for being low-quality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.