관찰자를 제목에 구독합니다.
네임스페이스:System.Reactive.Subjects
어셈블리: System.Reactive(System.Reactive.dll)
구문
'Declaration
Public Function Subscribe ( _
observer As IObserver(Of T) _
) As IDisposable
'Usage
Dim instance As AsyncSubject
Dim observer As IObserver(Of T)
Dim returnValue As IDisposable
returnValue = instance.Subscribe(observer)
public IDisposable Subscribe(
IObserver<T> observer
)
public:
virtual IDisposable^ Subscribe(
IObserver<T>^ observer
) sealed
abstract Subscribe :
observer:IObserver<'T> -> IDisposable
override Subscribe :
observer:IObserver<'T> -> IDisposable
public final function Subscribe(
observer : IObserver<T>
) : IDisposable
매개 변수
- 관찰자
형식: System.IObserver<T>
제목을 구독하는 관찰자입니다.
반환 값
형식: System.IDisposable
주체에서 관찰자를 구독 취소하는 데 사용할 수 있는 IDisposable 개체입니다.
구현
IObservable<T>. 구독(IObserver<T>)
설명
AsyncSubject의 Subscribe 메서드는 AsyncSubject의 관찰 가능한 시퀀스에 관찰자 구독을 추가하는 데 사용됩니다. AsyncSubject는 IObserver가 구독을 완료하는 OnComplete 호출을 받은 후에만 해당 IObservable 인터페이스에 게시됩니다. 이 문제가 발생하면 AsyncSubject에 대한 새 구독도 해당 구독에 게시되는 최종 결과를 갖게 됩니다.
예제
이 예제에서는 AsyncSubject를 사용하여 Range 연산자로 생성된 정수 시퀀스를 구독합니다. AsyncSubject는 구독된 시퀀스가 완료된 경우에만 값을 반환합니다. 시퀀스가 완료되면 AsyncSubject는 시퀀스에서 최종 항목을 게시합니다. AsyncSubject는 최종 항목을 캐시합니다. 해당 AsyncSubject에 대한 모든 새 구독에는 최종 항목도 해당 구독에 게시됩니다.
using System;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Concurrency;
using System.Threading;
namespace Example
{
class Program
{
static void Main()
{
//*******************************************************************************************************//
//*** A subject acts similar to a proxy in that it acts as both a subscriber and a publisher ***//
//*** It's IObserver interface can be used to subscribe to multiple streams or sequences of data. ***//
//*** The data is then published through it's IObservable interface. ***//
//*** ***//
//*** In this example an AsyncSubject is used to subscribe to an integer sequence from the Range ***//
//*** operator. An AsyncSubject only returns a value when the sequence it is subscribed to completes. ***//
//*** Once the sequence has completed, the AsyncSubject will publish the final item in the sequence. ***//
//*** The AsyncSubject caches the final item. Any new subscriptions against that AsyncSubject will ***//
//*** also have the final item published to that subscription as well. ***//
//*******************************************************************************************************//
var intSequence = Observable.Range(0, 10, Scheduler.ThreadPool);
AsyncSubject<int> myAsyncSubject = new AsyncSubject<int>();
intSequence.Subscribe(myAsyncSubject);
Thread.Sleep(1000);
myAsyncSubject.Subscribe(i => Console.WriteLine("Final integer for subscription #1 is {0}\n", i),
() => Console.WriteLine("subscription #1 completed.\n"));
Console.WriteLine("Sleeping for 5 seconds before subscription2\n");
Thread.Sleep(5000);
myAsyncSubject.Subscribe(i => Console.WriteLine("Final integer for subscription #2 after 5 seconds is {0}\n", i),
() => Console.WriteLine("subscription #2 completed.\n"));
Console.WriteLine("Press ENTER to exit...");
Console.ReadLine();
myAsyncSubject.Dispose();
}
}
}
다음 출력은 예제 코드에 의해 생성되었습니다.
Final integer for subscription #1 is 9
subscription #1 completed.
Sleeping for 5 seconds before subscription2
Final integer for subscription #2 after 5 seconds is 9
subscription #2 completed.
Press ENTER to exit...