简体   繁体   English

从Parse获取关系用户

[英]Getting relational users from Parse

I am attempting to determine if the current user is in a relationship with my event. 我试图确定当前用户是否与我的事件有关系。

In other words, I have events that users can join. 换句话说,我有用户可以加入的事件。 I store a PFRelation of User in the Event Table. 我在事件表中存储了用户的PFRelation。

I have tried 我努力了

self.event.users.query().getObjectInBackground(withId: currentUser.objectId!, block: { (object, error) in
    if let error = error {
        //The query returned an error
        print(error.localizedDescription)
    } else {
        //The object has been retrieved
        print(object)
    }
})

and

do {
    let user = try self.event.users.query().getObjectWithId(currentUser.objectId!)

    if user == nil {
        currentUser.saveInBackground { (success: Bool, error: Error?) in
            eventsRelation.add(object)
        }

        usersRelation.add(currentUser)
        object.saveInBackground()


    }
} catch {
    print("Unexpected error: \(error).")
}

But these each return the user no matter what. 但无论如何,这些都会返回用户。 Even if they are not in the relation. 即使他们不在关系中。

It is as if they are running the query on the whole user table. 就好像他们在整个用户表上运行查询一样。

How can I run it on just my subset? 我怎样才能在我的子集上运行它?

The answer is to use the relationship to query on. 答案是使用关系来查询。

See here for more documentation 有关更多文档,请参见此处

So the code would look like this: 所以代码看起来像这样:

let usersRelation = event.relation(forKey: "users")

let usersRelationQuery = usersRelation.query()

usersRelationQuery.whereKey("objectId", equalTo: currentUser.objectId!)
usersRelationQuery.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
    if let error = error {
        print(error.localizedDescription)
    } else if let objects = objects {
        // The find succeeded.
        print("Successfully retrieved \(objects.count) scores.")
        // Do something with the found objects
        for object in objects {
            print(object.objectId as Any)
        }
    }
})

Note that the event variable above is from the database. 请注意,上面的event变量来自数据库。

If we did not include the whereKey portion, then it would just get the ones in that relationship. 如果我们没有包含whereKey部分,那么它只会得到那个关系中的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM