didNotMatchKey:inQuery:返回与iOS的Parse SDK中的键匹配的记录 [英] doesNotMatchKey:inQuery: return records that match key in Parse SDK for iOS

查看:101
本文介绍了didNotMatchKey:inQuery:返回与iOS的Parse SDK中的键匹配的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Parse SDK在xcode中进行简单查询,向用户展示产品,但我不想向他们展示他们已经喜欢"的产品.

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."

因此我在使用Parse时有两个表:
-产品:具有objectId
-点赞:包含以下列:点赞(说是/否"),指向产品的指针,指向用户的指针

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

任何我的代码是:

//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.

如果我在SQL中执行此操作,我会写类似这样的内容:

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)

任何帮助将不胜感激!

谢谢,蒂姆

推荐答案

使用Parse和其他NoSQL服务,您需要从SQL心态中解放出来(使用规范化等). NoSQL的方案"更多地关注查询效率,而不是存储和一致性.正如Petro所建议的那样,您可以将所有喜欢的内容存储在用户表中的数组中.然后,您可以使用约束条件查询Parse,该约束条件指出记录不应与查询中的任何objectid匹配,如下所示:

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];

其中productLikes是产品ID的数组.

where productLikes is the array of product ids.

另一种解决方案是将产品喜欢的商品存储在另一个表中,但仍存储为数组(每个喜欢的商品没有单独的记录,并带有指向产品和用户的指针,就像使用SQL数据库那样). 在这种情况下,您可以像在问题中一样嵌套查询.

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.

这篇关于didNotMatchKey:inQuery:返回与iOS的Parse SDK中的键匹配的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆