0

I currently start with my first parse steps, but currently i stuck on a very basic point. Is there any way to get back a array with a list of all my "objectID"s from my "parseObject"?

I simply want a array that get all the automatic set objectID from one "table"

3 Answers 3

3

Here is a Swift solution:

        // objectIds is your array to store the objectId values returned from parse
    // objectId is a String

    var objectIds:[""] // empty string array
    func loadDataFromParse () {
        var query = PFQuery(className:"Record")
        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]!, error: NSError!) -> Void in
            if error == nil {
                // The find succeeded.
                println("Successfully retrieved \(objects.count) scores.")
                // Do something with the found objects
                for object in objects {
                    objectIds.append(object.objectId as String)
                }
            } else {
                println("\(error)")
            }

        }

    }


// this function will retrieve a photo in the record with specified objectId
// and store it in noteImage

    var noteImage = UIImage() // where retrieved image is stored
    func loadImageFromParse (objectId: String) {

        var query = PFQuery(className:"Record")
        query.whereKey("objectId", equalTo:objectId)
        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]!, error: NSError!) -> Void in
            if error == nil {
                println("Successfully retrieved \(objects.count) records.")
                for object in objects {
                    let userImageFile = object["image"] as PFFile!
                    userImageFile.getDataInBackgroundWithBlock {
                        (imageData: NSData!, error: NSError!) -> Void in
                        if error == nil {
                            noteImage = UIImage(data:imageData)!
                            println("Image successfully retrieved")
                        }
                    }
                }

            } else {
                NSLog("Error: %@ %@", error, error.userInfo!)
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

I dont think Parse provides a way to get all objectId's in an array. Alternatively you can iterate through each object and retrieve objectId's as:

    var objectIds = [String]()

    let query = PFQuery(className:"TableName")
    query.findObjectsInBackgroundWithBlock {(objects: [PFObject]?, error: NSError?) -> Void in

        if error == nil {
            if let objects = objects {
                for object in objects {
                   objectIds.append(String(object.valueForKey("objectId")!))
                }
            }
        } else {
            print("Error: \(error!) \(error!.userInfo)")
        }

        print(objectIds)
    } 

Comments

0

If I understand your question correctly you need to make a query of your objects, for example:

PFQuery *userPhotosQuery = [PFQuery queryWithClassName:@"photos"];
[userPhotosQuery whereKey:@"user"equalTo:[PFUser currentUser]];
[userPhotosQuery orderByDescending:@"createdAt"];

This will return all photo Objects, which was saved by current user. You can add any other filters as well. Please correct me If I'm wrong or don't catch the question correctly.

1 Comment

You should translate it to swift since that's what the question title states, and the OP has tagged swift. The OP may know how to translate it to swift, however you answered in Obj-C so future question-seekers that click on a title that says "get ObjectIDs with Swift" won't have the same conclusion as the OP

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.