Created
May 27, 2018 18:12
-
-
Save joseprl89/b07f68a4c999692c4901a425af9c13ee to your computer and use it in GitHub Desktop.
How LiveData observes a value for Internals of Android Architecture Components Part II- LiveData
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@MainThread | |
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) { | |
if (owner.getLifecycle().getCurrentState() == DESTROYED) { | |
// ignore | |
return; | |
} | |
LifecycleBoundObserver wrapper = new LifecycleBoundObserver(owner, observer); | |
ObserverWrapper existing = mObservers.putIfAbsent(observer, wrapper); | |
if (existing != null && !existing.isAttachedTo(owner)) { | |
throw new IllegalArgumentException("Cannot add the same observer" | |
+ " with different lifecycles"); | |
} | |
if (existing != null) { | |
return; | |
} | |
owner.getLifecycle().addObserver(wrapper); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment