1

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.

3
  • Do you want the query to return Conversation object that has ALL the users in "theUsers" stored in kWSConversationUsersKey ? So, if a conversation includes both "Lisa" and "Dennis", you want to return the conversation, but not if the conversation includes "Lisa", "Dennis" and "Mike"? And also not if it contains only "Lisa"? Commented Feb 10, 2014 at 10:20
  • Are you getting anywhere on this, @ramsel? Commented Feb 12, 2014 at 11:44
  • @MariusFalkenbergWaldal Sorry, S.O. started filtering in my e-mail. So your first comment is exactly what I want. For now I've just created an extra field that lists the numUsers in the conversation. I'll put it in an answer below. It works, but not sure if it's the most efficient solution. Commented Feb 13, 2014 at 19:16

1 Answer 1

1

So here's my workaround:

+ (PFQuery *)queryForConversation:(NSArray*)theUsers {

/////////////////////////////////
// First query for the conversation
PFQuery *conversationQuery = [PFQuery queryWithClassName:kWSConversationClassKey];
[conversationQuery whereKey:kWSConversationUsersKey containsAllObjectsInArray:theUsers];
[conversationQuery whereKey:KWSConversationNumUsersKey equalTo:[NSNumber numberWithInt:[theUsers count]]];
[conversationQuery whereKey:KWSConversationLanguageKey equalTo:[[WSWordlistManager shared] languageTarget]];
[conversationQuery includeKey:[NSString stringWithFormat:@"%@.%@",kWSConversationMessagesKey,kWSMessageUserKey]]; // Double power pointer retrieval!

return conversationQuery;

}

I have an extra key in my Conversation class that holds the number of users in the conversation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.