how to use retrofit in work manager to post some local messages to server one by one?
Asked Answered
B

0

6

In my chat app, I want to read messages from the local database and send them to the server by the work manager and retrofit when the user sends new messages.

in parent :

 OneTimeWorkRequest workRequest = new OneTimeWorkRequest
        .Builder(SendMsg_Work.class)
        .setConstraints(new Constraints
                .Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .build())
        .setInitialDelay(5, TimeUnit.SECONDS)
        .setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 30, TimeUnit.SECONDS)
        .build();
    WorkManager.getInstance().enqueue(workRequest);

in worker :

@NonNull
@Override
public Result doWork() {
        ChatRoom_Model_Local ll = database.get_unSendMsg();
        if (ll != null) {
            sendMessage(ll);
        } else {
            return Result.failure();
        }
}

and retrofit - i use RxJava too :

private void sendMessage(ChatRoom_Model_Local model) {

    Single<Response<MSG_Response>> api = ApiClient.createService(ApiInterface.class, pr.getData(pr.SendMessage_url))
            .sendMSG(pr.getData(pr.MY_TOKEN), model.getOther_user(), model.getMsg_text());

    api.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new SingleObserver<Response<MSG_Response>>() {
                @Override
                public void onSubscribe(Disposable d) { }

                @Override
                public void onSuccess(Response<MSG_Response> response) {
                    if (response.isSuccessful() && response.body() != null) {
                        String message = response.body().getMessage();
                        Intent bi = new Intent(new Config().getCHAT_SERVICE());
                        if (message != null && message.equals("xyx")) { // INSERT Successfully
                            String server_id = response.body().getId();
                            String localId = response.body().getIdLocal();

                            bi.putExtra("ChatRoomService_id", localId);
                            bi.putExtra("ChatRoomService_s_id",server_id);

                            int result = database.setMsgServerId(localId,server_id);

                        }
                        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(bi);

                    }

                    ChatRoom_Model_Local ll = database.get_unSendMsg();
                    if (ll != null) {
                        sendMessage(ll);
                    }
                }

                @Override
                public void onError(Throwable e) {
                }

            });
}

Now the problem is:

  1. How to return some Result in doWork method while retrofit is still in progress?

  2. What if the worker has already been called and the retrofit is in progress and the worker is called again?

  3. How can I send messages one after another to the server?

Thank you

Biceps answered 21/9, 2019 at 6:21 Comment(2)
WorkManager already executes on Background thread, so you can make synchronous call there rather than asynchronous.Burck
Please refer here, you may get some idea. proandroiddev.com/…Nadiya

© 2022 - 2024 — McMap. All rights reserved.