0

I am trying to update an array column in a Parse class (i.e. column name is friendsRequestList) with an object. I searched and only could find one way to do so which is:

let query = PFQuery(className: "FriendsConnections")
           query.getObjectInBackgroundWithId(self.friendObject.objectId!) {
            (user: PFObject?, error: NSError?) -> Void in
            if error != nil {
                print(error)
            } else if let user = user {
                user.addObject(self.userObject, forKey: "friendsRequestList")
                user.saveInBackground()
            }
        }

The only issue here is that function getObjectInBackgroundWithId requires object ID and I would like to have a query.wherekey instead as it works more with data I have in Parse.

How can I do the above without using objectID but using a whereKey instead.

Thanks

Update: New code based on answer provided but still didn't work:

let query = PFQuery(className: "FriendsConnections")
        query.whereKey("appUsername", equalTo: self.friendObject.username!)
        query.findObjectsInBackgroundWithBlock {
            (objects: [PFObject]?, error: NSError?) -> Void in

            if error == nil {
                // Do something with the found objects
                if let objects = objects {
                    for object in objects {
                        print("Processing Object")
                        object.addObject(PFUser.currentUser()!["appUsername"], forKey: "friendsRequestList")
                        object.saveInBackground()
                    }
                }
            } else {
                // Log details of the failure
                print("Error: \(error!) \(error!.userInfo)")
            }
        }
4
  • I am kind of lost... what are you trying to do ? Commented Jan 17, 2016 at 15:51
  • Update a column of array type without having ObjectID Commented Jan 17, 2016 at 16:05
  • what is friendObject ? Commented Jan 17, 2016 at 16:15
  • Ignore it because it wouldn't be relevant in the solution to the problem since I will not use ObjectID. Just assume I want to update a column where a username matches the supplied argument Commented Jan 17, 2016 at 16:17

1 Answer 1

1

I think I understand what you're saying, so here's my go at it:

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

  if error == nil {
    // The find succeeded.
    print("Successfully retrieved \(objects!.count) scores.")
    // Do something with the found objects
    if let objects = objects {
      for object in objects {
        object.addObject(self.userObject, forKey: "friendsRequestList")
        object.saveInBackground()
      }
    }
  } else {
    // Log details of the failure
    print("Error: \(error!) \(error!.userInfo)")
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried the code (modified the fields of course) and updated original post above. Still didn't work. It is not printing Processing Object statement which means it is not going into the if. no errors
@ksa_coder And can you verify that there are objects in your database that match the query constraints?
got it working. Silly error from my side. Thanks for helping out mate :)

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.