0

I want to be able to delete a column value for several different users in the Parse user class. This is a code that explains a little bit:

PFQuery *query = [PFUser query];
[query whereKey:@"objectId" containedIn:usersArray]; //Finding the wanted users
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        //Here I would like to delete all values for all users inside the "image" column
        //How can I do that?
    }
}];

I hope you understand what I want.

1 Answer 1

2

If you weren't trying to update the PFUser object, you could simply iterate through them one by one, and remove the object for the column.

PFQuery *query = [PFQuery queryWithClassName:"AnyNonUserOrInstallationClass"];
[query whereKey:@"objectId" containedIn:usersArray];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        for (PFObject *object in objects) {
          [object removeObjectForKey:@"image"];
        }
        // And then you can save all the updated objects via a single call to Parse.com
        [PFObject saveAllInBackground:objects block:^(BOOL success, NSError *error) {
          // Check success/error.
        }];
    }
}];

Because you're updating the PFUser, you need to use the master key. You can do this by creating a cloud code function, and saving the user objects in there.

Cloud code

var _ = require('underscore');

Parse.Cloud.define("removeUserImages", function(request, response) {
  var query = new Parse.Query(Parse.User);
  // Add your criteria for selection, usersArray needs to be passed into the function
  query.containedIn("objectId", request.params["usersArray"]);
  query.find().then( function(users) {
    _.each(users, function(user) {
      user.unset("image");
    });
    Parse.Cloud.useMasterKey();
    return Parse.Object.saveAll(users);
  }).then(function(result) {
    response.success("Success");
  }, function(error) {
    response.error("Couldn't remove images from users");
  });
});

Objective-C to call cloud function

// oid1, oid2 should be the objectIds of the users you want to remove images from
[PFCloud callFunctionInBackground:@"removeUserImages" withParameters:@{@"usersArray" : @["oid1", "oid2"]} block:^(id result, NSError *error){
  if (!error) {
  }
}];

Note: None of the above is tested. I've just typed it in here.

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

6 Comments

I get an error saying: User cannot be saved unless they have been authenticated via logIn or signUp. Can't I save a user unless I'm logged in as that user?
Ah, yes, makes sense. IIRC the PFUser object has a special security restriction on it, so it can only be saved if you're logged in as that user. Unless, you're using the master key. So, what you're probably going to have to do is create a custom cloud code function to run that query, remove the image object, and save the users with the master key in there.
The above should work for objects that aren't User or Installation, or have restrictive ACL's defined.
How can you use the master key? I haven't heard about it? Cause that would work for me.
Seems like this is going to work! Thanks mate! But do you have a method to add @"oid1" and so on automatically depending on the usersArray? Cause [usersArray objectAtIndex:0] is obviously @"oid1" and so on..
|

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.