Reputation: 11796
In an android app , is AppObservable a MUST for each observable ?
I have a retrofit generated Observable , injected by Dagger :
public class UserInfoFragment extends Fragment {
@Inject
Observable<User> user;
}
In this example
should I override the 'user' in the onViewCreated()
like this ?
this.user = AppObservable.bindFragment(this , user);
Moreover , if I don't inject Observable directly , instead , I inject an Object containing an Observable
:
@Inject
CachedValue<User> cachedUser;
And in the CachedValue , there is an Observable (other fields skipped) :
public class CachedValue<T> {
public Observable<T> getObservable() {
return networkObservable.startWith(localObservable).take(1);
}
}
Should I replace this Observable with AppObservable lifted Observable ?
AppObservable.bindFragment(this , cachedUser.getObservable());
All these codes run flawlessly without AppObservable in the simulator (I did unsubscribe these subscriptions in onDestroyView()
), but I don't know in real situation is it enough ? Will there be any memory leak after long run ?
In the rx-android's sample project , I saw AppObservable everywhere. But I don't know how to handle injected Observables (or objects containing Observables) correctly .
Thanks a lot .
Upvotes: 2
Views: 1246
Reputation: 11880
No, I don't think AppObservable is a must.
If you check AppObservable source code, it's pretty clear that AppObservable helps ensure that no item will be forwarded to a fragment after it's already destroyed/detached.
From my understanding, you can call isUnsubscribed before emitting an item along with unsubscribing subscriptions in onDestroyView. That should be suffice.
See also:
Upvotes: 3