我學會了it is undesirable在反應式編程中使用Subjects
,儘管我發現它們非常方便。但我知道他們可能會被濫用。所以我試圖創建一個無限Observable<ImmutableMap<Integer,ActionProfile>
,每調用一次refresh()
就需要發佈一個新的ImmutableMap
。我也有一個forKey()
方法返回Observable
檢索最新的ActionProfile
匹配一個特定的鍵。RxJava-無法訪問Observable的訂戶?
然而,事情只是不覺得如何處理用戶的猶太潔食。如果可觀察者的生命是無限的,我是否認爲你必須在Observable的構造之外自己管理用戶? Observable是否保留其用戶列表?或者是我的責任,所以我可以隨時致電onNext()
?
public final class ActionProfileManager {
private final Observable<ImmutableMap<Integer,ActionProfile>> actionProfiles;
private volatile ImmutableMap<Integer,ActionProfile> actionProfileMap;
//do I really need this?
private final CopyOnWriteArrayList<Subscriber<? super ImmutableMap<Integer,ActionProfile>>> subscribers = new CopyOnWriteArrayList<>();
private ActionProfileManager() {
this.actionProfiles = Observable.create(subscriber -> {
subscriber.onNext(actionProfileMap);
subscribers.add(subscriber); // is it up to me to capture the subscriber here or is it already saved somewhere for me?
});
}
public void refresh() {
actionProfileMap = importFromDb();
subscribers.forEach(s -> s.onNext(actionProfileMap));
}
public Observable<ActionProfile> forKey(int actionProfileId) {
return actionProfiles.map(m -> m.get(actionProfileId));
}
private ImmutableMap<Integer,ActionProfile> importFromDb() {
return ImmutableMap.of(); //import data here
}
}
我沒有足夠的經驗給主管回答你的問題,但:1)你會發現這個答案,我對CR有趣的問題:HTTP ://codereview.stackexchange.com/a/90090/68342 2.)你可能想看看源代碼sqlbrite,特別是這個文件,它在內部使用'Subject'來處理重載/觸發器:https:// github。 com/square/sqlbrite/blob/master/sqlbrite/src/main/java/com/squareup/sqlbrite/SqlBrite.java –