我有一個Rx管道,根據傳入數據創建一些實體,然後應該保留這些實體並在完成狀態時打印。如何避免在Rx管道中重新執行操作
我試過下面的代碼。問題在於它多次重新執行操作,而不是僅僅一次,因此我在數據庫中獲得了多個實體副本。基本上用下面的代碼,我想打印「堅持test1」只有一次。
var test1Observable = Observable.Return("test1");
var test2Observable = Observable.Return("test2");
var createTest1Observable = test1Observable.Select(name => name);
var test1 = createTest1Observable.Do(x => Console.WriteLine("persisting test1"));
var createTest3Observable = Observable.CombineLatest<string, string, Tuple<string,string>>(test1, test2Observable, (t1, t2) => new Tuple<string,string>(t1,t2));
var persistTest3Observable = createTest3Observable.Do(x => Console.WriteLine("persisting test 3"));
var createTest4Observable = test1.Select(x => "test4");
var persistTest4Observable = createTest4Observable.Do(x => Console.WriteLine("persisting test 4"));
var createTest5Observable = test1.Select(x => "test5");
var persistTest5Observable = createTest5Observable.Do(x => Console.WriteLine("persisting test5"));
var OnSuccesObservable = Observable.CombineLatest<string, Tuple<string,string>, string, string, bool>(test1, persistTest3Observable, persistTest4Observable, persistTest5Observable, (t1, t2, t3, t4) =>
{
if (t1 == null || t2 == null || t3 == null || t4 == null)
return false;
else
return true;
});
OnSuccesObservable.Subscribe(
o =>
{
Console.WriteLine("Finished");
Console.ReadLine();
});
'var persistTest2Entity = createTest1Entity.Do(persistor);' - 應該是createTest * 2 * Entity 。做(...)? –
是的,對不起,我現在編輯並修正了這個問題中的拼寫錯誤。但它並沒有解決問題。 – user3115696
我意識到我的初始代碼示例有點太簡化了,並沒有顯示出那麼好的問題,所以我編輯了它,以便它顯示得更好。代碼多次打印「持久test1」,但我希望它只發生一次。 – user3115696