Fabian Boulegue
Fabian Boulegue

Reputation: 6608

Parse.com get ObjectIDs with Swift

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"

Upvotes: 0

Views: 4411

Answers (3)

Syed Tariq
Syed Tariq

Reputation: 2928

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

Upvotes: 3

Adnan Asghar
Adnan Asghar

Reputation: 341

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

Upvotes: 1

David V
David V

Reputation: 2144

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.

Upvotes: 0

Related Questions