作者:我就是老笨2013 | 来源:互联网 | 2023-02-12 14:40
1> Grisgram..:
与所有异步操作一样,我经常以回调方式执行此操作.
您的解决方案可以这样工作:
创建一个简单的回调接口(我在几乎每个应用程序中使用的库中都有我的)
public interface SimpleCallback {
void callback(Object data);
}
// You could do it as well generic, that's what I do in my lib:
public interface SimpleCallback {
void callback(T data);
}
然后重新设计方法的签名,如下所示:
private void checkAnswerSubmission(@NonNull SimpleCallback finishedCallback) {
DatabaseReference answerDatabase = FirebaseDatabase.getInstance().getReference().child("userPuzzleHistory").child(uid);
answerDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This will simple call your callback containing a boolean true/false
finishedCallback.callback(dataSnapshot.hasChild(String.valueOf(imagename)));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
你这称之为:
checkAnswerSubmission(new SimpleCallback() {
@Override
void callback(boolean data) {
if (data) {
// true was returned
} else {
// false was returned
}
}
});