Alek Piasecki Alek Piasecki - 4 months ago 16
Swift Question

How to keep parse-server entries unique when added from swift iteration?

I am coding an app which will update entries in a database (parse-server, hosted on heroku) when a button is clicked. It saves two things; the user's object ID (unique for each user) and the object (a thing the user clicks on)'s object Id (unique for each object). It is done roughly as such:

@IBAction func goButton(_ sender: AnyObject) {

let goingSelection = PFObject(className: "GoingTo")
goingSelection["userGoingID"] = (PFUser.current()?.objectId)! as String
goingSelection["objectID"] = objectIDLabel.text


It works fine in adding the data the DB. However, it can do so multiple times for the same userId/objectId pairing (because it is added as a new objectt (two t's for differentiating)) and this objectt has its own unique objecttId. Thus, the same userId/objectId is saved to a different objecttId upon each click. I would like the DB addition to be exclusively unique for the userId/objectId pairing. Is there anyway to do this?


You have to process yourself this validation serverside within a beforeSave hook on your GoingTo class, eg:

Parse.Cloud.beforeSave("GoingTo", function(request, response)
    var goingTo = request.object
    // Create a query matching object you are trying to create
    var alreadyExistingQuery = Parse.Query...
    alreadyExistingQuery.equalTo ...
    return alreadyExistingQuery.first().then(function(alreadyExistingObject){
        if(alreadyExistingObject == null)
            response.error("This object already exists")

(Possible typos errors, writing this from head)