解析查询覆盖旧查询的新查询?

Parse Query overrides new query over old one?

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

在我的应用程序中,用户可以阻止其他用户。完成后,我将 "Block" 对象写入数据库,如下所示

我屏蔽了其他用户:

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

有效。我该如何解决这个问题。

每个键只能添加一个 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);

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

根据此(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);