Al Lelopath
Al Lelopath

Reputation: 6778

Fragment subscribe to Observer

I'm trying to implement an Observer/Subscriber with RxJava for the first time. I get the compile error:

cannot resolve method subscribe(android.support.v4.app.Fragment) 

on the line indicated below. So I'm not subscribing correctly. How do I do this?

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        Fragment myFragment = mTabsPageAdapter.getItem(2);

        Observable<String> loadAndStoreDataObservable = Observable.create(
                new Observable.OnSubscribe<String>() {
                    @Override
                    public void call(Subscriber<? super String> subscriber) {
                        try {           
                            <get data from RESTful service>
                            <write data to SQLite db on device>

                            subscriber.onNext("Done");
                            subscriber.onCompleted();
                        }
                        catch (Exception e) {
                            subscriber.onError(e);
                        }
                    }
                }
        )
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(myFragment); // cannot resolve method subscribe(android.support.v4.app.Fragment)
    }
}

import android.support.v4.app.Fragment;
public class MyFragment extends Fragment implements Observer<String> {

        @Override                         
        public void onNext(String s) {    
            System.out.println(s);        
        }                                 

        @Override                         
        public void onCompleted() {       
        }                                 

        @Override                         
        public void onError(Throwable e) {
        }
}      

Edit: I changed the line suggested by Vladimir Mironov. This seems to be necessary but not sufficient. Implementing it I then get a compile error on the line after that:
Incompatible types: Required rx.Observable Found rx.Subscription

It suggests casting to (Observable<String>) like so:

Observable<String> loadAndStoreDataObservable = (Observable<String>) Observable.create(...)

which does indeed compile error-free, but gives the runtime error:
java.lang.ClassCastException: rx.observers.SafeSubscriber cannot be cast to rx.Observable

Edit 2: I think it should be: Subscription loadAndStoreDataObservable = ...

Upvotes: 1

Views: 3879

Answers (1)

Al Lelopath
Al Lelopath

Reputation: 6778

No one is stepping forward, so I'll aggregate the comments into an answer.

  1. Cast to MyFragment:

    MyFragment myFragment = (MyFragment) mTabsPageAdapter.getItem(2);
    
  2. Change loadAndStoreDataObservable to be a Subscription

    Subscription loadAndStoreDataObservable = ...
    
  3. In OnDestroy(), unsubscribe:

    protected void onDestroy() {  
        super.onDestroy();  
        if (loadAndStoreDataObservable != null) {  
           loadAndStoreDataObservable.unsubscribe();  
        }
    }
    

Upvotes: 3

Related Questions