Swift - 太复杂的查询Parse(OR带有> 4个子预测)

时间:2016-01-28 16:22:58

标签: ios swift parse-platform predicate pfquery

我正在尝试为多个用户创建聊天室。 所以,我所做的是创建一个类(" Lists"),添加用户(" user1" - " user10")然后当然给了他们objectId。 我现在做的是检查类中的用户是否与当前用户匹配,以便仅在其中一个用户帐户上显示此类。我用谓词做了那个。问题是谓词格式无法处理带有> 4个子预测值的OR(这也是Parse文档指南中的内容)。

当我拿出5-10个用户时,一切都按照应有的方式工作,问题是该组最多只能有四个用户。如果有可能避免在当前状态下使用pred,那么一切都将是完美的。

错误(在控制台中):

  

此查询过于复杂。它之后有一个带有> 4个子预测的OR   正常化。

这是我的代码:

   func loadGroups() {

    grooms = [PFObject]()

    users = [PFUser]()

    self.tableView.reloadData()

    let pred = NSPredicate(format: "user1 = %@ OR user2 = %@ OR user3 = %@ OR user4 = %@ OR user5 = %@ OR user6 = %@ OR user7 = %@ OR user8 = %@ OR user9 = %@ OR user10 = %@", PFUser.currentUser() !, PFUser.currentUser() !, PFUser.currentUser() !, PFUser.currentUser() !, PFUser.currentUser() !, PFUser.currentUser() !, PFUser.currentUser() !, PFUser.currentUser() !, PFUser.currentUser() !, PFUser.currentUser() !)

    let groomQuery = PFQuery(className: "Lists", predicate: pred)

    groomQuery.orderByAscending("lastUpdate")

    groomQuery.includeKey("user1")

    groomQuery.includeKey("user2")

    if groomQuery.includeKey("user3") != "" {

      groomQuery.includeKey("user3")

    }

    if groomQuery.includeKey("user4") != "" {

      groomQuery.includeKey("user4")

    }

    if groomQuery.includeKey("user5") != "" {

      groomQuery.includeKey("user5")

    }

    if groomQuery.includeKey("user6") != "" {

      groomQuery.includeKey("user6")

    }

    if groomQuery.includeKey("user7") != "" {

      groomQuery.includeKey("user7")

    }

    if groomQuery.includeKey("user8") != "" {

      groomQuery.includeKey("user8")

    }

    if groomQuery.includeKey("user9") != "" {

      groomQuery.includeKey("user9")

    }

    if groomQuery.includeKey("user10") != "" {

      groomQuery.includeKey("user10")

    }

    SwiftLoader.show(true)

    groomQuery.findObjectsInBackgroundWithBlock {
      (results: [AnyObject] ? , error : NSError ? ) - > Void in
        if error == nil {

        SwiftLoader.hide()

        print("found everything", terminator: "")

        self.grooms = results as![PFObject]

        for groom in self.grooms {

          let user1 = groom.objectForKey("user1") as!PFUser

          let user2 = groom["user2"] as!PFUser

          if groom["user3"] != nil {

            self.user3 = groom["user3"] as!PFUser

          }

          if groom["user4"] != nil {

            self.user4 = groom["user4"] as!PFUser

          }

          if groom["user5"] != nil {

            self.user5 = groom["user5"] as!PFUser

          }

          if groom["user6"] != nil {

            self.user6 = groom["user6"] as!PFUser

          }

          if groom["user7"] != nil {

            self.user7 = groom["user7"] as!PFUser

          }

          if groom["user8"] != nil {

            self.user8 = groom["user8"] as!PFUser

          }

          if groom["user9"] != nil {

            self.user9 = groom["user9"] as!PFUser

          }

          if groom["user10"] != nil {

            self.user10 = groom["user10"] as!PFUser

          }

          if user1.objectId != PFUser.currentUser() !.objectId {

            self.users.append(user1)

            print("append user1", terminator: "")

          }

          if user2.objectId != PFUser.currentUser() !.objectId {

            self.users.append(user2)

          }

          if self.user3 != nil {

            if self.user3.objectId != PFUser.currentUser() !.objectId {

              self.users.append(self.user3)

            }

          }

          if self.user4 != nil {

            if self.user4.objectId != PFUser.currentUser() !.objectId {

              self.users.append(self.user4)

            }

          }

          if self.user5 != nil {

            if self.user5.objectId != PFUser.currentUser() !.objectId {

              self.users.append(self.user5)

            }

          }

          if self.user6 != nil {

            if self.user6.objectId != PFUser.currentUser() !.objectId {

              self.users.append(self.user6)

            }

          }

          if self.user7 != nil {

            if self.user7.objectId != PFUser.currentUser() !.objectId {

              self.users.append(self.user7)

            }

          }

          if self.user8 != nil {

            if self.user8.objectId != PFUser.currentUser() !.objectId {

              self.users.append(self.user8)

            }

          }

          if self.user9 != nil {

            if self.user9.objectId != PFUser.currentUser() !.objectId {

              self.users.append(self.user9)

            }

          }

          if self.user10 != nil {

            if self.user10.objectId != PFUser.currentUser() !.objectId {

              self.users.append(self.user10)

            }

          }

        }

        self.tableView.reloadData()

        self.groomArray = self.grooms

      }
    }

最相关的一行:

let pred = NSPredicate(format: "user1 = %@ OR user2 = %@ OR user3 = %@ OR user4 = %@ OR user5 = %@ OR user6 = %@ OR user7 = %@ OR user8 = %@ OR user9 = %@ OR user10 = %@", PFUser.currentUser()!, PFUser.currentUser()!, PFUser.currentUser()!, PFUser.currentUser()!, PFUser.currentUser()!, PFUser.currentUser()!, PFUser.currentUser()!, PFUser.currentUser()!, PFUser.currentUser()!, PFUser.currentUser()!)

我的问题:

是否可以避免和交换上面的代码行以使其工作?或者通过关系来做这一切会更有效率吗?有什么提示吗?

提前致谢,对于有关代码或任何问题的任何问题,请发表评论。

0 个答案:

没有答案