gpt4 book ai didi

java - 聊天应用程序: Get messages between 2 users in Parse query

转载 作者:行者123 更新时间:2023-12-01 13:03:27 25 4
gpt4 key购买 nike

我正在使用 Parse 构建一个聊天应用程序。我想检索两个用户之间的聊天记录。在 SQL 中,它可以这样表示:

SELECT * FROM Message WHERE (sender = user1 AND receiver = user2) OR (sender = user2 AND receiver = user1)

但我不知道如何转换为解析查询。

这是我的消息模型:

import java.util.Date;

import com.parse.ParseClassName;
import com.parse.ParseObject;
import com.parse.ParseUser;

@ParseClassName("Message")
public class Message extends ParseObject {

public Message() {

}

public String getId() {
return getObjectId();
}

public Date getDate() {
return getCreatedAt();
}

public ParseUser getSender() {
return getParseUser("sender");
}

public void setSender(ParseUser user) {
put("sender", user);
}

public ParseUser getReceiver() {
return getParseUser("receiver");
}

public void setReceiver(ParseUser user) {
put("receiver", user);
}

public String getContent() {
return getString("content");
}

public void setContent(String content) {
put("content", content);
}
}

最佳答案

您可以使用类似的东西:

String user1;
String user2;

// build first AND condition
ParseQuery<ParseObject> queryPart1 = ParseQuery.getQuery("Message");
queryPart1.whereEqualTo("sender", user1);
queryPart1.whereEqualTo("receiver", user2);

// build second AND condition
ParseQuery<ParseObject> queryPart2 = ParseQuery.getQuery("Message");
queryPart2.whereEqualTo("sender", user2);
queryPart2.whereEqualTo("receiver", user1);

// list all queries condition for next step
List<ParseQuery<ParseObject>> queries = new ArrayList<ParseQuery<ParseObject>>();
queries.add(queryPart1);
queries.add(queryPart2);

// Compose the OR clause
ParseQuery<ParseObject> innerQuery = ParseQuery.or(queries);
innerQuery.addAscendingOrder("createdAt"); // apply sorting for creation date ascending (use addDescendingOrder for Desc sorting)

// *** Parse does not allow selection result greater than 1000 elements for query, so you may need to paging your system
//innerQuery.setSkip(10); // skip the first 10 results
//innerQuery.setLimit(200); // limit selection to first 200 elements ( from your skip offset if set, from the first otherwise )

// Run selection asynchronously
innerQuery.findInBackground(new FindCallback<ParseObject>() {

public void done(List<ParseObject> results, ParseException e) {
// Do your stuff with 'results' list, if no error has been occurred
// ...
// ..
// .
}

});

希望对你有帮助!

关于java - 聊天应用程序: Get messages between 2 users in Parse query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23379150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com