您好我正在使用 Parse 和 Firebase 构建一个聊天应用程序,用户详细信息在 Parse 中维护,并且聊天在 Firebase 上运行。
每次用户开始与某人聊天时,它必须去 Parse "Recent" - 类,它必须检查 Arraylist 匹配,如果该行包含匹配,那么它必须返回一个预期值,我将保存它在共享首选项中,否则我们在代码本身中创建一个值并将其保存在首选项中。
我的问题是...实际上 Parse 查询工作正常,它第一次立即传递预期值,但下一次(如果你回去选择另一个用户聊天),它从共享首选项,然后执行解析查询,因为解析线程在后台执行,因此需要时间。因此,第二次聊天也保存在以前的值中。我只想立即从解析查询中获取值并保存它。但我不知道该怎么做。
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
groupIdQuery = ParseQuery.getQuery("Recent");
groupIdQuery.whereContainsAll("members", Arrays.asList(user1ObjectId, user2ObjectId));
Log.d("objectArray", Arrays.asList(user1ObjectId, user2ObjectId).toString());
groupIdQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null && list.size() != 0) {
for (ParseObject dealsObject : list) {
recentClassObjectId = dealsObject.getObjectId();
groupId = (String) dealsObject.get("groupId");
Log.d("!!!!!GroupId", groupId);
//String members = (String) dealsObject.get("members");
//Toast.makeText(getApplicationContext(), "ObjectId: " + recentClassObjectId + "\n" + "GroupId: " + groupId + " value present", Toast.LENGTH_LONG).show();
}
editor = sharedpreferences.edit();
editor.putString(gId, groupId);
Log.d("ifGroupId", groupId);
editor.commit();
} else {
SharedPreferences.Editor editor = sharedpreferences.edit();
Toast.makeText(getApplicationContext(), " No Data present", Toast.LENGTH_LONG).show();
groupId = user1ObjectId + user2ObjectId;
editor.putString(gId, groupId);
editor.commit();
Log.d("!!!!ElseGroupId", groupId);
entryToRecentChat = new ParseObject("Recent");
entryToRecentChat.put("groupId", groupId);
entryToRecentChat.put("lastUser", ParseObject.createWithoutData("_User", user1ObjectId));
entryToRecentChat.put("user", ParseObject.createWithoutData("_User", user2ObjectId));
entryToRecentChat.addAllUnique("members", Arrays.asList(user1ObjectId, user2ObjectId));
entryToRecentChat.saveEventually(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(getApplicationContext(), "Value saved", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Value not saved", Toast.LENGTH_LONG).show();
Log.d("exception", e.getMessage());
}
}
});
Log.d("elseGroupId", groupId);
editor = sharedpreferences.edit();
editor.putString(gId, groupId);
Log.d("ifGroupId", groupId);
editor.commit();
}
}
});
valueId = sharedpreferences.getString(gId, "default");
mFirebaseRef = new Firebase(FIREBASE_URL).child(valueId);
Log.d("FinalGroupId", valueId);
谁能告诉我如何解决这个问题?