Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Issue with ReplaySubject and Take #292

Open
tgreener opened this issue Mar 8, 2018 · 1 comment · May be fixed by #462
Open

Issue with ReplaySubject and Take #292

tgreener opened this issue Mar 8, 2018 · 1 comment · May be fixed by #462

Comments

@tgreener
Copy link

tgreener commented Mar 8, 2018

I have a situation where I'm using a ReplaySubject and Take(int). Here's a minimal example of how to reproduce the problem:

ReplaySubject<string[]> rep = new ReplaySubject<string[]>(1);
rep.OnNext(new[] { "start" });
rep
  .Take(1)
  .Subscribe(strings => {
    var list = strings.ToList();
    list.Add("added");
    rep.OnNext(list.ToArray());
  });

And here are the relevant parts of the exception that is thrown:

InvalidOperationException: Collection was modified after the enumerator was instantiated.
System.ThrowHelper.ThrowInvalidOperationException (System.ExceptionResource resource) (at <3649452ae23840d49bd323a708498f78>:0)
System.Collections.Generic.Queue`1+Enumerator[T].MoveNext () (at <3649452ae23840d49bd323a708498f78>:0)
UniRx.ReplaySubject`1[T].Subscribe (System.IObserver`1[T] observer) (at Assets/Plugins/UniRx/Scripts/Subjects/ReplaySubject.cs:170)
UniRx.Operators.TakeObservable`1[T].SubscribeCore (System.IObserver`1[T] observer, System.IDisposable cancel) (at Assets/Plugins/UniRx/Scripts/Operators/Take.cs:58)
UniRx.Operators.OperatorObservableBase`1[T].Subscribe (System.IObserver`1[T] observer) (at Assets/Plugins/UniRx/Scripts/Operators/OperatorObservableBase.cs:34)
UniRx.ObservableExtensions.Subscribe[T] (System.IObservable`1[T] source, System.Action`1[T] onNext) (at Assets/Plugins/UniRx/Scripts/Observer.cs:413)

If I remove the .Take(1) (and the inner OnNext to avoid an infinite loop), everything works as expected.

Luckily, since I'm basically using a ReplaySubject to act like a BehaviorSubject, I can just change to using one of those (no problem there, it seems), but this was still annoying.

System Info:
iMac (27-inch, Late 2013)
macOS 10.12.6
Unity version 2017.3.1f1 personal
Scripting Runtime version .NET 4.6 equivalent
API Level .NET 4.6

reluctantmedia added a commit to reluctantmedia/UniRx that referenced this issue Jun 23, 2020
@reluctantmedia reluctantmedia linked a pull request Jun 23, 2020 that will close this issue
@reluctantmedia
Copy link

I have submitted a PR #462 to address this issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants