Reputation: 141
I have a android project and am using wait() and notify() so certain code executes only after a background task,this is a function that makes calls to firebase:
//Code Snippet 1
public Task<String> checkIfJokeSavedFirebase() throws InterruptedException {
final String[] idToken = {""};
Map<String, Object> data = new HashMap<>();
FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();
mUser.getIdToken(true)
.addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
public void onComplete(@NonNull Task<GetTokenResult> task) {
if (task.isSuccessful()) {
idToken[0] = task.getResult().getToken();
data.put("token", idToken[0]);
String someObject = "test";
synchronized (someObject) {
Log.d("longdebug","someObject pre notify");
someObject.notify();
}
//notify();
// Send token to your backend via HTTPS
// ...
} else {
// Handle error -> task.getException();
}
}
});
String someObject = "test";
synchronized (someObject){
Log.d("longdebug","someObject pre wait");
someObject.wait();
}
Log.d("runtimeexception","pre actually called");
data.put("token", idToken[0]);
//data.put("jokeid",jokeJSON.getString("id"));
return FirebaseFunctions.getInstance()
.getHttpsCallable("checkIfJokeSaved")
.call(data)
.continueWith(new Continuation<HttpsCallableResult, String>() {
@Override
public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
Log.d("runtimeexception",".then");
HashMap result = (HashMap) task.getResult().getData();
JSONObject res = new JSONObject(result);
String jokeStored = res.getString("jokeStored");
Log.d("runtimeexception","jokeStored returned");
return jokeStored;
}
});
}
And my code that calls the function::
//Code Snippet 2
try {
Log.d("runtimeexception","pre checkSavedCall");
activity.checkIfJokeSavedFirebase().addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
Log.d("runtimeexception","pre task.getResult");
Log.d("runtimeexception","task.getResult:" + task.getResult());
jokeSaved = Boolean.parseBoolean(task.getResult());
String synchronizedChannel = "displaysaved";
synchronized (synchronizedChannel){
Log.d("runtimeexception","pre notify");
synchronizedChannel.notifyAll();
}
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
String synchronizedChannel = "displaysaved";
synchronized (synchronizedChannel){
try {
Log.d("runtimeexception","pre wait");
synchronizedChannel.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
My code doesn't make it to the ".then" log in code snippet 1,but when I remove:
synchronized (synchronizedChannel){
try {
Log.d("runtimeexception","pre wait");
synchronizedChannel.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
from snippet 2,snippet 1 makes it to the ".then" log and completes executing. How is possible that removing a wait() statement results in a call to a firebase function finishing?
Upvotes: 0
Views: 90
Reputation: 141
I didn't find the solution to the Synchronized problem, but I just switched from AsyncTask to RxJava,and moved the Firebase calls to a Task,which allowed me to get rid of all of the previous synchronized statements,and only required two synchronized statements in the Task.
Upvotes: 1