0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

IObservableとIObserverについて

Posted at

最初に

WPFでMVVMを行う際、よくIObservableやIObserverについてよく記事が出てくるので、自分のメモのつもりで書きました。

  • 参考にしたサイト(こちらを最初ご覧ください)

Reactive Extensions再入門 その2「IObservableインターフェースとIObserverインターフェース」
https://blog.okazuki.jp/entry/20111101/1320156608
公式IObservable
https://learn.microsoft.com/ja-jp/dotnet/api/system.iobservable-1?view=net-7.0
公式IObserve
https://learn.microsoft.com/ja-jp/dotnet/api/system.iobserver-1?view=net-7.0

IObserveについて

namespace System
{
    public interface IObserver<in T>
    {
        void OnNext(T value);
        void OnError(Exception error);
        void OnCompleted();
    }
}
  • データを変更を受信する
  • OnNextで新しいデータを通知する
  • onErrorでプロバイダーでエラーが発生したことを通知する
  • OnCompletedでデータの送信が完了したことを通知する

IObservableについて

namespace System
{
    public interface IObservable<out T>
    {
        IDisposable Subscribe(IObserver<T> observer);
    }
}
  • IObservableは、データ通知を行う提供者(プロバイダー)と呼ばれる
  • SubscribeでIObserverに登録を行う
  • 登録したIObserverに対して、データ変更の通知を行う

まとめ

  • IObservable → IObserve にデータを提供する流れ
  • SubscribeでObserveを登録して、Execute()などメソッドを作成して、そこでObserveのOnNextを呼び出す
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?