【问题标题】:Parse.com Android : Compound QueriesParse.com Android:复合查询
【发布时间】:2015-01-12 13:34:18
【问题描述】:

我想在 Android 应用程序中从 Parse.com 数据库中获取数据。

在我的表格中,我有以下列:

file_Type String、Message_File File、Text_Messages String、RecipientID String、SenderID String、Created_At、Updated_At

现在我想查找userID 的数据,比如A123,它存在于RecipientIDSenderID 中。

我试过了:

    ParseQuery<ParseObject> messageQueryRecv = new ParseQuery<ParseObject>("Messages");
    messageQueryRecv.whereEqualTo(kosherinfotech.NGCApp.workers.Constants.RECIPIENT_IDS, currentUser.getObjectId());

    ParseQuery<ParseObject> messageQuerySend = new ParseQuery<ParseObject>("Messages");
    messageQuerySend.whereEqualTo(Constants.SENDER_ID,currentUser.getObjectId());

    List<ParseQuery<ParseObject>> queries = new ArrayList<ParseQuery<ParseObject>>();
    queries.add(messageQueryRecv);
    queries.add(messageQuerySend);

    ParseQuery<ParseObject> mainQuery = ParseQuery.or(queries);
    mainQuery.addDescendingOrder("createdAt");

【问题讨论】:

  • 输出/错误是什么?什么是预期的?
  • 感谢您的回复。代码中没有错误,但它不返回任何内容。我需要发送者或接收者是当前用户的所有行。

标签: android parse-platform


【解决方案1】:

如果您上面的代码是完整的代码,那么您永远不会真正查询 Parse 数据库。您需要在代码底部使用类似的内容来异步检索结果。

mainQuery.findInBackground(new FindCallback<ParseObject>() {
    public void done(List<ParseObject> results, ParseException e) {
        // Results will contain the list of matches. 
        if(e == null ){
            // Matches found
        } else {
            // An error occured 
        }         
    }
});

【讨论】:

  • 感谢您的回复。如何将两个查询中的所有唯一行选择到一个结果集中。即假设我想在第 1 列或第 2 列中找到用户 A 条目。
  • 您刚刚描述的内容与您最初的问题不符,请您决定您真正想要什么。查找唯一行与上面的完全不同
  • 我给出的代码应该可以找到两个查询的结果。结果的对象可以在 done 回调函数的结果参数中找到。您只需在问题中添加的代码下方添加我给您的代码
【解决方案2】:
ParseQuery<ParseObject> messageQuerySend = new ParseQuery<ParseObject>      ("Messages");

messageQuerySend.whereEqualTo(Constants.SENDER_ID,currentUser.getObjectId());

到目前为止这是正确的,但是在查询执行并将结果发送回客户端应用程序之后,您需要添加 JayDev 在他的答案中给出的代码,或者您可以

List<Message> messagesList= messageQuerySend.find(); //without the call back.

让我知道会发生什么。

【讨论】:

  • 在 findInBackground 上使用 find() 通常是不好的做法,因为 find() 是一个同步方法,这意味着除非调用该方法的线程已经在后台任务中,否则它将导致 UI冻结直到查询返回
【解决方案3】:

在构建之后,您构建了查询对象,您通过调用 findInbackground 调用 query 并在回调函数中处理结果

ParseQuery<ParseObject> messageQueryRecv = new ParseQuery<ParseObject>("Messages");
messageQueryRecv.whereEqualTo(kosherinfotech.NGCApp.workers.Constants.RECIPIENT_IDS, currentUser.getObjectId());
messageQueryRecv.addDescendingOrder("createdAt");
messageQueryRecv.findInBackground(new FindCallback<ParseObject>() {

 void done(List<ParseObject> results, ParseException e) {
  //do something with the result
  }
 });


ParseQuery<ParseObject> messageQuerySend = new ParseQuery<ParseObject>("Messages");
messageQuerySend.whereEqualTo(Constants.SENDER_ID,currentUser.getObjectId());
messageQuerySend.addDescendingOrder("createdAt");
messageQuerySend.findInBackground(new FindCallback<ParseObject>() {

 void done(List<ParseObject> results, ParseException e) {
  //do something with the result
  }
 });

【讨论】:

  • 我想要一个结果集中的两个查询的结果集,我不想单独处理它们。
  • 我正在添加我的查询。没有具有相同 Constants.RECIPIENT_IDS 和 Constants.SENDER_ID 的数据库条目现在我需要检查 OR 条件。即我的用户应该是列之一,我无法触发或条件我希望我现在清楚
【解决方案4】:

您可能必须在主查询中使用 .include(Constants....)。如果您将 ParseObjects 嵌套在其他 ParseObjects 中,则不会自动获取它们。您可以使用 .include 让 SDK 知道也可以获取它们,或者您可以在从主查询中获取对象后手动获取它们。

取自 Parse.com

在某些情况下,您希望在一个查询中返回多种类型的相关对象。您可以使用 include 方法执行此操作。例如,假设您正在检索最后十个 cmets,并且您想同时检索他们的相关帖子:

ParseQuery<ParseObject> query = ParseQuery.getQuery("Comment");

// Retrieve the most recent ones
query.orderByDescending("createdAt");

// Only retrieve the last ten
query.setLimit(10);

// Include the post data with each comment
query.include("post");

query.findInBackground(new FindCallback<ParseObject>() {
  public void done(List<ParseObject> commentList, ParseException e) {
    // commentList now contains the last ten comments, and the "post"
    // field has been populated. For example:
    for (ParseObject comment : commentList) {
      // This does not require a network access.
      ParseObject post = comment.getParseObject("post");
      Log.d("post", "retrieved a related post");
    }
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 2016-05-18
    • 1970-01-01
    相关资源
    最近更新 更多