解析IOS删除key = objectID的所有对象

Apo*_*llo 5 objective-c ios parse-platform

是否有一种简单的方法可以删除满足特定条件的特定类(即消息)中的所有对象,例如"UserID"= user,以便删除与特定用户关联的消息类中的所有行?

Akh*_*jtr 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]);
 }
}];
Run Code Online (Sandbox Code Playgroud)

更新

更换

for (PFObject *object in objects) {
    [object deleteInBackground];
}
Run Code Online (Sandbox Code Playgroud)

[PFObject deleteAllInBackground:objects];
Run Code Online (Sandbox Code Playgroud)

感谢mikewoz的更新.

  • 不要在for循环中使用[object deleteInBackground],而应该使用[PFObject deleteAllInBackground:objects]在一个请求中完成所有操作.有关此电话的更多信息,请访问:http://blog.parse.com/2013/07/22/deleting-multiple-objects-in-one-api-request/ (4认同)
  • 有没有更有效的方法来执行此操作,以便您不必启动查询来查找对象? (3认同)