从查找表

时间:2015-09-29 18:21:08

标签: ios objective-c parse-platform pfquery

我尝试编写PFQuery以将用户的朋友作为用户对象。下面显示的是我的User表和Friend Lookup表。当用户创建友谊时,会在Friend Lookup表中添加一个条目。

DB Structure

我的想法是构建一个查询,查询Lookup表中sendingUserreceivingUser等于当前用户的任何行。

NSString * const kPULLookupSendingUserKey = @"sendingUser";
NSString * const kPULLookupReceivingUserKey = @"receivingUser";
....
PULUser *acct = [PULUser currentUser];
// query where sending user is current user
PFQuery *senderQuery = [PFQuery queryWithClassName:@"FriendLookup"];
[senderQuery whereKey:kPULLookupSendingUserKey equalTo:acct];

// query where receiving user is current user
PFQuery *recQuery = [PFQuery queryWithClassName:@"FriendLookup"];
[recQuery whereKey:kPULLookupReceivingUserKey equalTo:acct];

// query that ORs the previous two together and adds a couple more constraints
PFQuery *lookupQuery = [PFQuery orQueryWithSubqueries:@[senderQuery, recQuery]];
[lookupQuery whereKey:@"isBlocked" equalTo:@NO];
[lookupQuery whereKey:@"isAccepted" equalTo:@YES];

如果我只运行lookupQuery,它会返回正确的行,其中当前用户是发送者或接收者。我希望将该行数组作为User对象而不是FriendLookup对象。

注意 PULUserPFUser

的子类
// query for user where sending user key matches OID
PFQuery *userQuerySender = [PULUser query];
[userQuerySender whereKey:@"objectId" matchesKey:kPULLookupSendingUserKey inQuery:lookupQuery];

// query for user where receiving user key matches OID
PFQuery *userQueryRec = [PULUser query];
[userQueryRec whereKey:@"objectId" matchesKey:kPULLookupReceivingUserKey inQuery:lookupQuery];

// join queries together in an OR
PFQuery *query = [PFQuery orQueryWithSubqueries:@[userQuerySender, userQueryRec]];
// exclude self user from query
[query whereKey:@"objectId" notEqualTo:acct.objectId];

这将返回一个空数组。有关如何正确形成此查询的任何想法?或者是否有一种不同的方法来存储友谊,这将使这项任务更容易。

1 个答案:

答案 0 :(得分:1)

使用include键,以便查询结果包含指向2个用户的指针,然后与所有用户组成一个集合并删除当前用户,然后您将拥有所有用户而无需进行辅助查询。