P
P
prizrak392018-12-10 07:54:16
Java
prizrak39, 2018-12-10 07:54:16

How to correctly pass the response to the UI after the file is loaded?

It was necessary to implement the upload of the user's file to the server. Implemented this using sockets in a separate thread. But since the run() method does not return any value, that is, I cannot notify the user about the result of the download. Perhaps I did not correctly understand the use of the RxJava library, but it is not entirely clear to me how to apply it to solve this problem. The only solution I found is AsyncTask.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2018-12-10
@prizrak39

In general, loading a file is a lengthy operation, and it would be better to fix it for such a WorkManager( https://developer.android.com/topic/libraries/arch...
But if you stubbornly do it on Rx, and stability is not so important, look at observeOn /subscribeOn.Switching threads is done with their
help.Don't even look in the direction of AsyncTasks, they are not for this at all.Some intern in Google invented them for "convenience", and they are still dragging them.

P
prizrak39, 2018-12-10
@prizrak39

This is what the implementation through Rx should look like?

String pathFile = "C:\1.txt"; 
        Observable<String> myObservable = Observable.create(
            new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> sub) {
                sub.onNext(pathFile);
                sub.onCompleted();
            }
        });
        
        Subscriber<String> mySubscriber = new Subscriber<String>() {
            @Override
            public void onNext(String s) { 
                // Upload file
            }

            @Override
            public void onCompleted() { }

            @Override
            public void onError(Throwable e) { }
        };
    
        myObservable.subscribe(mySubscriber);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question