【问题标题】:Programmatically deleting data associated with PFUser.objectID if PFUser is deleted如果 PFUser 被删除,则以编程方式删除与 PFUser.objectID 关联的数据
【发布时间】:2016-01-08 04:47:27
【问题描述】:

所以在我的应用程序中,我将 Parse 用于用户帐户。我有一个类“Follow”,其中包含用户对象 ID 的“to”和“from”字段,允许用户互相关注。现在,如果我以某种方式删除用户,则跟随关系保持不变,并且查询空用户数据会引发对象未找到错误。我想知道的是,如果任何“to”或“from”字段包含不存在的对象的 objectID,我如何删除关注关系。

我曾尝试通过 objectID 查询对象,但任何检查空数据的尝试(例如检查 user.username 是否为 nil)都会导致缺少对象错误,并且我无法检查对象是否为 nil,因为Xcode 说它永远不会。

谢谢!

【问题讨论】:

  • 你是手动删除用户吗?
  • 是的,但我只是在问,以防万一我想实现 deleteUser() 方法。但我想在这种情况下,我可以在删除用户之前删除任何相关的关注关系。所以我可能不应该担心这个吧?
  • 为此您必须使用云代码.....实现删除前触发器

标签: ios swift parse-platform pfobject pfuser


【解决方案1】:

我认为您需要手动获取它,如果您已删除用户,则可以删除其所有关系以维护数据库完整性,以下是您在删除任何用户后调用的代码。

PFQuery * _to = [PFQuery queryWithClassName:@"Follow"];
[_to whereKey:@"to" equalTo:@"Replace UserID of Deleted User"];

PFQuery * _from = [PFQuery queryWithClassName:@"Follow"];
[_from whereKey:@"from" equalTo:@"Replace UserID of Deleted User"];

PFQuery *query = [PFQuery orQueryWithSubqueries:@[_to, _from]];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
  if (!error) {
     // The find succeeded.
     NSLog(@"Successfully retrieved %d relations.", results.count);
     // Do something with the found objects, lets delete them all to intact relationship
     [PFObject deleteAllInBackground:results];
 } else {
     // Log details of the failure
     NSLog(@"Error: %@ %@", error, [error userInfo]);
 }
}];

请点击此链接了解详细说明和信息:
Parse Compound Queries

【讨论】:

    猜你喜欢
    • 2010-12-29
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多