T - Indicates the type of data to observe.public class ActiveData<T> extends Object
ActiveData can be used to save Component data in the Model or record database data in the Model.
ActiveData is essentially a data class, and observers are added to observe its data changes.
When a data change occurs, the ActiveData object can notify its observers of the change.
While the ActiveData is being observed, it also observes lifecycle changes of the DataObserver.
Generally, a DataObserver can be notified of data changes only when the AbilitySlice hosting the
observer is in the foreground (ACTIVE state) since it does not need to know real-time data changes in other
states. After it returns to the foreground, the DataObserver will be notified the most recent data changes.
ActiveData can also notify an observer of data changes even when the AbilitySlice is in the
background state. To enable this function, set the always parameter in the
addObserver(ohos.aafwk.abilityjet.activedata.DataObserver,boolean) method to true when you add the observer.
You do not need to maintain the observers. When the lifecycle of an AbilitySlice is destroyed, the
related observer is automatically removed. Therefore, ActiveData can be seen as an observable data type
that can observe lifecycle states. The biggest advantage of ActiveData is that it serves as a lifecycle
observer and notifies other observers of data changes based on the lifecycle of an object.
Example code of using ActiveData:
public class MainAbility extends Ability {
private ActiveData fruitPrice;
@Override
protected void onStart() {
super.onStart();
subscribe();
}
@Override
protected void onActive() {
if(fruitPrice.getData().empty()) {
// setData will notify Observer in subscribe.
fruitPrice.setData(String("10"));
}
private void subscribe() {
final DataObserver priceObserver = new DataObserver() {
@Override
public void onChanged(@Nullable final String s) {
String price = "Fruit price is " + s;
Text.setText(price);
}
};
fruitPrice.addObserver(priceObserver);
}
}
}
SystemCapability.Aafwk#ABILITYJET| 构造器 | 说明 |
|---|---|
ActiveData() |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
addObserver(DataObserver<T> observer,
boolean always) |
Adds a data observer.
|
T |
getData() |
Obtains the active data.
|
void |
removeObserver(DataObserver<T> observer) |
Removes a data observer.
|
void |
setData(T value) |
Sets the data to observe.
|
public void addObserver(DataObserver<T> observer, boolean always)
After a data observer is added, it will be notified when data is changed.
observer - Indicates the data observer to add for receiving data change notifications. The value cannot be
null.always - Specifies whether to send data change notifications in any lifecycle state. The value
true indicates that notifications are sent in any lifecycle state. The value false
indicates that notifications are sent only when the object is in the foreground (ACTIVE
state).IllegalArgumentException - invalid parameterpublic void removeObserver(DataObserver<T> observer)
After a data observer is removed, it will no longer be notified of data changes.
observer - Indicates the data observer to remove.public void setData(T value)
value - Indicates the data to observe.public T getData()