0

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

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

我阻止其他用户:

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

作品。我怎样才能解决这个问题。

4

2 回答 2

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);
于 2019-05-13T18:24:01.860 回答
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);
于 2019-05-14T13:48:03.150 回答