Closed amrro closed 7 years ago
NetworkBoundResources is not applicable with POST
request for me. I created similar class with follow difference:
@MainThread
ChangeAction(AppExecutors appExecutors) {
mAppExecutors = appExecutors;
makeChange();
}
private void makeChange() {
LiveData<ApiResponse<RequestType>> apiResponse = createCall();
apiResponse.observeForever(response -> {
if (response.isSuccessful()) {
mAppExecutors.diskIO().execute(() -> {
saveCallResult(processResponse(response));
mAppExecutors.mainThread().execute(() -> {
mResult.addSource(loadFromDb(), newData -> mResult.setValue(Resource.success(newData)));
});
});
} else {
onChangeFailed();
mResult.addSource(loadFromDb(), newData -> mResult.setValue(Resource.error(response.error, newData)));
}
});
}
Be careful! I didn't test this code, and it may have bugs.
I suggest to override saveCallResult(@NonNull RequestType item)
instead of adding more code. So, every Call in any repository handle its case.
@amrro could you elaborate on your response or provide an example?
What is the solution to this post request problem? Is there any google recommendation?
@plabon Similar to GithubService linked earlier, OkHttp/Retrofit also provides an @POST
annotation.
@dlam No i was asking about using NetworkBoundResources How can I put data in local database and post the data to server?
TL;DR: How to use
POST
instead ofGET
?Thanks for your effort, really great learning experience browsing your code. But I have small question.
What I've noticed that all requests in
GithubService
areGET
type. By using the abstract classNetworkBoundResource
to load each object by overridingcreateCall()
in its Repository, likeUserRepository#loadUser(String login)
.In
Post
, I update data remotely on server which I might have instance of it locally with Room, What/How to update Room depending on the API? Is the same class,NetworkBoundResource
, is applicable withPOST
requests? What is the best scenario in this case?Thanks!