Is there an easy way to delete all objects within a particular class (i.e. messages) which satisfy a particular condition, such as "UserID" = user, so that all of the rows within my message class associated with a particular user will be deleted?
2
-
You will need to elaborate further, I have no idea what you're talking about.random– random2014-02-13 20:01:20 +00:00Commented Feb 13, 2014 at 20:01
-
What particular class are you using? Sample code will help us help you.Phillip Kinkade– Phillip Kinkade2014-02-13 20:01:37 +00:00Commented Feb 13, 2014 at 20:01
Add a comment
|
2 Answers
Try this,
PFQuery *query = [PFQuery queryWithClassName:@"messages"];
[query whereKey:@"UserID" equalTo:@"user"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(@"Successfully retrieved %d scores.", objects.count);
// Do something with the found objects
for (PFObject *object in objects) {
[object deleteInBackground];
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
Update
replace
for (PFObject *object in objects) {
[object deleteInBackground];
}
with
[PFObject deleteAllInBackground:objects];
thanks mikewoz for the update.
4 Comments
Apollo
Is there a more effective way to do this so that you don't have to launch a query to find the objects?
Apollo
this seems query intensive
Marius Waldal
This is a very sought-after functionality among the community, but Parse has yet to implement it. The solution @Akhilrajtr offers is quite bandwidth-intensive if there are many objects. To alleviate that, you could create a cloud code function to handle it all in the backend.
mikewoz
Instead of using [object deleteInBackground] in a for loop, you should use [PFObject deleteAllInBackground:objects] to do it all in one request. More info on this call here: blog.parse.com/2013/07/22/…
This helped me finally figure out how to delete all related pointer objects of a deleted object while using Parse and the local store.
Hope this saves someone a few hours.
Parse.Cloud.afterDelete("Student", function(request) {
// after delete a student find the associated sessions and remove
var query = new Parse.Query("Session");
var userPointer = {
__type: 'Pointer',
className: 'Student',
objectId: request.object.id
}
query.equalTo("studentOwner", userPointer);
query.find().then(function(studentSessions) {
return Parse.Object.destroyAll(studentSessions);
}).then(function(success) {
// The related sessions were deleted
}, function(error) {
console.error("Error deleting related sessions " + error.code + ": " + error.message);
});
});