Hi I am building a chat application using Parse and Firebase, User details are maintained in Parse and the chat is working on Firebase.
In that every time user start to chat with someone it has to go to Parse "Recent" - class and it has to check for the Arraylist match, if the row contains a match then it has to return a expected value and I will save that in a shared preference, else we create a value in the code itself and save it in preference.
What my problem is... Actually Parse query is working fine and its passing the expected value for the first time immediately, but for the next time (If u go back and select another user to chat), its taking the previous value from the shared preference and then its executing the Parse query, because parse thread is executing in background so its taking time. Because of that the second chat is also saving in the previous value. I just wanna get the value from parse query immediately and save it. But I don't know how to do that.
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);
Can anyone, tell me how to solve this?
Disclosure: I work for Firebase
I've never worked with Parse before, but like most of the modern internet it probably loads data asynchronously. This means that you should only try to load the data from Firebase, when Parse is done loading its data.
You do this by moving the code that loads from Firebase into the callback that is executed the Parse data has loaded:
Note that I removed all the code that is unrelated to the actual problem, so that it's easier to see the flow.