解析查询会覆盖旧查询中的新查询吗?

时间:2019-05-12 11:01:05

标签: parse-platform parse-server parse-android-sdk

我正在解析查询以获取数据,但是新的查询参数会覆盖旧的参数。

在我的应用中,用户可以阻止其他用户。完成后,我将这样的“块”对象写入数据库

我屏蔽了其他用户:

ParseObject block = new ParseObject("Block");                                
block.put("owner",ParseUser.getCurrentUser());                                                
block.put("who",otherUser);

当我获取数据时,我将使用此查询。

    //I blocked him. dont sow me his data.
    ParseQuery<ParseObject> benblock= new ParseQuery<ParseObject>("Block");
    benblock.whereEqualTo("owner", ParseUser.getCurrentUser());

    //He blocked me dont show me his data.
    ParseQuery<ParseObject> oblock = new ParseQuery<ParseObject>("Block");                
    oblock.whereEqualTo("who",ParseUser.getCurrentUser());

    query.whereDoesNotMatchKeyInQuery("user","owner",oblock);  //This line doesn't work                
    query.whereDoesNotMatchKeyInQuery("user","who",benblock);  //But this line does.

但是当我检索数据时,第一个查询选项

query.whereDoesNotMatchKeyInQuery("user","owner",oblock); 

仅最后一个无效

 query.whereDoesNotMatchKeyInQuery("user","who",benblock);

有效。我该如何解决。

2 个答案:

答案 0 :(得分:0)

每个键只能添加whereDoesNotMatchKeyInQuery一个。当您为同一键添加两个具有相同约束的约束时,第二个约束实际上将覆盖第一个约束。因此,我建议您使用Parse.Query.and,如下所示:

//I blocked him. dont sow me his data.
ParseQuery<ParseObject> benblock = new ParseQuery<ParseObject>("Block");
benblock.whereEqualTo("owner", ParseUser.getCurrentUser());

//He blocked me dont show me his data.
ParseQuery<ParseObject> oblock = new ParseQuery<ParseObject>("Block");                
oblock.whereEqualTo("who", ParseUser.getCurrentUser());

ParseQuery<ParseObject> query1 = new ParseQuery<ParseObject>("YouDataClass");
query1.whereMatchesKeyInQuery("user","owner",oblock);  // you will need to create two queries
ParseQuery<ParseObject> query2 = new ParseQuery<ParseObject>("YouDataClass");
query2.whereMatchesKeyInQuery("user","who",benblock);

List<ParseQuery<ParseObject>> queries = new ArrayList<ParseQuery<ParseObject>>();
queries.add(query1);
queries.add(query2);
ParseQuery<ParseObject> orQuery = ParseQuery.or(queries);

ParseQuery<ParseObject> mainQuery = new ParseQuery<ParseObject>("YouDataClass");
mainQuery.whereDoesNotMatchQuery("objectId", orQuery);

答案 1 :(得分:0)

我通过这样做解决了我的问题。

根据此(https://github.com/parse-community/Parse-SDK-Android/issues/211#issuecomment-150047052)解析查询覆盖只对同一键进行whereDoesNotMatchKeyInQuery的操作。这意味着我可以在同一查询中两次使用该参数,但不能使用同一键。我只是用不同的键创建了相同的值。

以前我的对象是这样的

    ParseObject object = new ParseObject("DataClass");
    object.put("post",post);
    object.put("user",user);

现在看起来像这样:

    ParseObject object = new ParseObject("DataClass");
    object.put("post",post);
    object.put("user",user);
    object.put("user2",user);//same user with the above one

我确实这样查询:

ParseQuery<ParseObject> benblock= new ParseQuery<ParseObject>("Block");
benblock.whereEqualTo("owner", ParseUser.getCurrentUser());

ParseQuery<ParseObject> oblock = new ParseQuery<ParseObject>("Block");
oblock.whereEqualTo("who",ParseUser.getCurrentUser());

hashtag.whereDoesNotMatchKeyInQuery("user","owner",oblock);
hashtag.whereDoesNotMatchKeyInQuery("user2","who",benblock);