0

I have a class on parse.com, that contains many objects. One of the keys of each object, is the name of a country. How can I programatically delete all objects with a repeated country name key. The only way I can think of is very longwinded, so any ideas would be greatly appreciated.

This is the best I could come up with.

var objectId = [String]()
        var Country = [String]()
        var city = [String]()
        var deletes = [String]()
        var query = PFQuery(className: "Route")
        query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
            for object in objects {
                objectId.append(object.objectId)
                Country.append(object["Country"] as String)
                city.append(object["Start"] as String)
            }
            println(objectId)
            println(Country)
            var i = Int()
            for i=0; i < objectId.count; i++ {

                var j = Int()
                for j = i + 1 ; j < objectId.count; j++ {
                    if Country[i] == Country[j] {
                        println(objectId[j])
                        var deleteQuery = PFQuery(className: "Route")
                        deleteQuery.getObjectInBackgroundWithId(objectId[j], block: { (object: PFObject!, error2: NSError!) -> Void in
                            if error2 == nil {
                                object.delete()
                            }
                        })

                    }
                }
            }
        }

I'm convinced there must be a simpler way.

2 Answers 2

1

I think you could make the duplicate search a little skinnier by creating an NSMutableSet of country names. Run through the returned objects adding the country names to the set, testing membership beforehand. If a an object's country is in the set already, add it to a mutable array of of duplicates.

The deletion part of your code can be much improved. After the set-based test fills the duplicates array, use PFObject(deleteAllInBackground:duplicates) This will replace all of the code beginning at for i=0; i < objectId.count; i++ {

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

1 Comment

Thanks for the reply, unfortunately I only started developing recently and am only familiar with swift, and [PFObject deleteAllInBackground:duplicates]; looks suspiciously like objective-C? I'm also not entirely sure where I would implement this. Thanks for any further help
1

The way you have done it is correct, just remember to change the limit from the default of 100 to 1000.

If you need to do it on a table/class with more than 1000 rows then you'll need to create a server-side job to do it and use the query.each() function to iterate over all the rows, then clean up the duplicates.

Comments

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.