doesNotMatchKey:inQuery: return records that match key in Parse SDK for iOS

戏子无情 提交于 2020-01-04 03:18:13

问题


I'm trying to do a simple query in xcode using the Parse SDK where I am showing products to users but i don't want to show them products they've already "liked."

So I have two tables in Parse that I'm using:
- Products: has an objectId
- Likes: contains columns: Like (says yes/ no), Pointer to a product, Pointer to a user

Any my code is:

//query will get products that the user has liked
PFQuery *likeQuery = [PFQuery queryWithClassName:@"Likes"];
[likeQuery whereKey:@"User_Obj" equalTo:[PFUser currentUser]];

//product query will get products that are not in like query
PFQuery *prodQuery = [PFQuery queryWithClassName:@"Products"];
[prodQuery whereKey:@"objectID" doesNotMatchKey:@"Product" inQuery:likeQuery];

//restrict to 10 items
[prodQuery setLimit:10];
[prodQuery findObjectsInBackgroundWithTarget:self
                                 selector:@selector(getCallback:error:)];

However, when I try and run this previously liked items by that user are being returned.

If i were doing this in SQL I would write something like:

SELECT product FROM product WHERE product NOT IN (SELECT product FROM likes WHERE useriD = currentUser AND like = yes)

Any help would be much appreciated!

Thanks, Tim


回答1:


Working with Parse and other NoSQL services, you need to free yourself from the SQL mindset (with normalization etc). NoSQL "schemas" are focused on query efficiency more than storage and consistency. You could, as Petro suggests, store all your likes in an array in the user table. You could then query Parse with a constraint stating that records should not match any objectid in your query, like this:

[query whereKey:@"objectId" notContainedIn:user.productLikes];

where productLikes is the array of product ids.

Another solution would be to store the product likes in a different table, but still as an array (not separate records for each like, with pointers to products and users like you would have done with a SQL database). In this case you could nest the queries much like you have in your question.



来源:https://stackoverflow.com/questions/21519862/doesnotmatchkeyinquery-return-records-that-match-key-in-parse-sdk-for-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!