Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

java - Firebase onComplete never called in Android 10

I want to change status(online/offline) in firebase realtime database from chat activity onResume and onPause method.

my code is below:

@Override
protected void onResume() {
    super.onResume();
    updateStatus(Constants.getInstance().userStatusOnline);
}

@Override
protected void onPause() {
    super.onPause();
    updateStatus(Constants.getInstance().userStatusOffline);
}

public void updateStatus(String status) {
    Log.e(TAG, "## updateStatus status : " + status);
    if (myUserId != null) {
        databaseReference = FirebaseDatabase.getInstance().getReference(Constants.getInstance().fb_table_user).child(myUserId);

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("status", status);

        databaseReference.updateChildren(hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                Log.e(TAG, "******* updateStatus onComplete isSuccessful :: " + task.isSuccessful());

            }
        });
    }
}

It is working fine in other android version but onComplete method is not getting called in Android 10. I have also tried to updateChildren in job scheduler and work manager but not worked.

I have read everywhere that the FirebaseDatabase operations are performed asynchronously on a background thread

Thank you in advance for help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have created work manager like below and overrides addOnFailureListener and addOnCanceledListener methods and its working now.

public class ChangeStatusScheduler extends Worker {

private String TAG = "ChangeStatusScheduler";
private String status,userId;


public ChangeStatusScheduler(@NonNull Context appContext, @NonNull WorkerParameters params) {
    super(appContext, params);
    status = params.getInputData().getString(Constants.getInstance().fb_user_status);
    userId = params.getInputData().getString(Constants.getInstance().fb_user_id);
}

@NonNull
@Override
public Result doWork() {
    Log.e(TAG, "******* doWork");
    try {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(Constants.getInstance().fb_user_status, status);

        DatabaseReference statusRef =  FirebaseDatabase.getInstance().getReference(Constants.getInstance().fb_table_user)
                .child(userId);

        statusRef.updateChildren(hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                Log.e(TAG, "******* onComplete");

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                e.printStackTrace();
                Log.e(TAG, "******* onFailure  ee :: " + e.getMessage());
            }
        }).addOnCanceledListener(new OnCanceledListener() {
            @Override
            public void onCanceled() {
                Log.e(TAG, "******* onCanceled");
            }
        });
    }catch (Exception ee){
        Log.e(TAG, "******* updateStatus ee :: " + ee.getMessage());
        ee.printStackTrace();
    }
    return Result.success();
}
}

and called that work manager like below

@Override
protected void onStop() {
    super.onStop();

    Log.e(TAG, "******* chat onStop backClicked :: " + backClicked);
    if (!backClicked) {
        try {
            Data inputData = new Data.Builder()
                    .putString(Constants.getInstance().fb_user_status, Constants.getInstance().userStatusOffline)
                    .putString(Constants.getInstance().fb_user_id, myUserId).build();

            final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(ChangeStatusScheduler.class).setInputData(inputData).build();
            WorkManager.getInstance().enqueue(workRequest);
        } catch (Exception ee) {
            Log.e(TAG, "******* updateStatus ee :: " + ee.getMessage());
            ee.printStackTrace();
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...