是否有一种简单的方法可以删除满足特定条件的特定类(即消息)中的所有对象,例如“UserID”= user,以便我的消息类中与特定用户关联的所有行都将被删除?
答案 0 :(得分:14)
试试这个,
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]);
}
}];
<强>更新强>
替换
for (PFObject *object in objects) {
[object deleteInBackground];
}
带
[PFObject deleteAllInBackground:objects];
感谢mikewoz的更新。
答案 1 :(得分:0)
这帮助我最终弄清楚如何在使用Parse和本地存储时删除已删除对象的所有相关指针对象。
希望这可以节省几个小时。
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);
});
});