system.reactive - Observable collection OnNext not firing -
i have simple observable collection, , onnext not firing.
list<int> intlist = new list<int>(){1,2,3}; iobservable<int> observablelist = intlist.toobservable(); idisposable subscription = observablelist.subscribe( x => console.writeline("received {0} source.", x), ex => console.writeline( "onerror: " + ex.message ), ( ) => console.writeline( "oncompleted" ) ); intlist.add(4);
the output getting follows.
received 1 source.
received 2 source.
received 3 source.
oncompleted
i expecting "received 4 source." after add 4 list.
can please throw light on doing wrong. new rx
this depends on order of operations.
if structure code this:
list<int> intlist = new list<int>() { 1, 2, 3 }; iobservable<int> observablelist = intlist.toobservable(); intlist.add(4); idisposable subscription = observablelist .subscribe( x => console.writeline("received {0} source.", x), ex => console.writeline("onerror: " + ex.message), () => console.writeline("oncompleted"));
...then works expect.
the issue .subscribe
run on current thread .toobservable()
. actual code run return (iobservable<tsource>) new toobservable<tsource>(source, schedulerdefaults.iteration);
. schedulerdefaults.iteration
current thread.
you can see this code:
list<int> intlist = new list<int>() { 1, 2, 3 }; iobservable<int> observablelist = intlist.toobservable(); console.writeline("before subscription"); idisposable subscription = observablelist .subscribe( x => console.writeline("received {0} source.", x), ex => console.writeline("onerror: " + ex.message), () => console.writeline("oncompleted")); console.writeline("after subscription, before add"); intlist.add(4); console.writeline("after add");
when run get:
before subscription received 1 source. received 2 source. received 3 source. oncompleted after subscription, before add after add
so .add
hasn't happened until after subscription complete.
now, if try around changing code intlist.toobservable(scheduler.default)
new problem. running above code this:
before subscription after subscription, before add after add received 1 source. onerror: collection modified; enumeration operation may not execute.
now clearly, have concurrency issue. shouldn't try manipulate collections , iterate them @ same time.
Comments
Post a Comment