So I have an array of pointers to PFUsers - theUsers. I want to query against a key in my Conversation class that contains exactly this - arrays of PFUsers.
So the problem is that I want to find Conversation objects that involve the users I'm passing and only them.
I tried the following lines but they don't return a match even if the arrays are identical:
[conversationQuery whereKey:kWSConversationUsersKey containedIn:theUsers];
[conversationQuery whereKey:kWSConversationUsersKey containsAllObjectsInArray:theUsers];
I also tried a simple whereKey:equalTo but of course got an exception saying I can't compare arrays directly.
Here's the full query:
- (PFQuery *)queryForConversation:(NSArray*)theUsers {
/////////////////////////////////
// First query for the conversation
PFQuery *conversationQuery = [PFQuery queryWithClassName:kWSConversationClassKey];
[conversationQuery whereKey:kWSConversationUsersKey containedIn:theUsers];
[conversationQuery whereKey:kWSConversationUsersKey containsAllObjectsInArray:theUsers];
[conversationQuery whereKey:KWSConversationLanguageKey equalTo:[[WSWordlistManager shared] languageTarget]];
[conversationQuery includeKey:kWSConversationMessagesKey];
// A pull-to-refresh should always trigger a network request.
[conversationQuery setCachePolicy:kPFCachePolicyNetworkOnly];
// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
//
// If there is no network connection, we will hit the cache first.
if (self.objects.count == 0 || ![[UIApplication sharedApplication].delegate performSelector:@selector(isParseReachable)]) {
[conversationQuery setCachePolicy:kPFCachePolicyCacheThenNetwork];
}
return conversationQuery;
}
My only other thought is that I can add a key to the Conversation class that is a number indicating how many participants there are. Then my two constraints can be:
[conversationQuery whereKey:kWSConversationUsersKey containsAllObjectsInArray:theUsers];
[conversationQuery whereKey:kWSConversationNumUsersKey equalTo:[NSNumber numberWithInt:[theUsers count]];
which seems like it would work, but I'm wondering if there's something easier I'm missing.
numUsersin the conversation. I'll put it in an answer below. It works, but not sure if it's the most efficient solution.