Use of the base class of RAC

RACSignal,RACSubject,RACReplaySubject RACSignal // **1. Create Signal** RACSignal *signal = [RACSignal createSignal:^...

RACSignal,RACSubject,RACReplaySubject
  • RACSignal

    // **1. Create Signal**
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
    // The block, which is called whenever the signal is subscribed to; used primarily to send data
    // **3. Send Data**
    [subscriber sendNext:@"123456"];
    // **3.1. Finished sending data**
    [subscriber sendCompleted];
    // Usually you can use return nil here; instead
    return [RACDisposable disposableWithBlock:^{
    // The block is called whenever the signal unsubscribes; used primarily to empty resources
    // **4.1. Empty Resources**
    NSLog(@"Signal resource emptied!");
    }];
    }];
    // **2. Subscription Signal**
    RACDisposable *disposable = [signal subscribeNext:^(id _Nullable x) {
    NSLog(@"%@", x);
    }];
    // **4. Call to empty resources**
    [disposable dispose];

  • RACSubject

    // **1. Create Signal**
    RACSubject *subject = [RACSubject subject];
    // **2. Subscription Signal**
    [subject subscribeNext:^(id _Nullable x) {
    NSLog(@"%@", x);
    }];
    // **3. Send Data**
    [subject sendNext:@"data"];

  • RACReplaySubject

    // **1. Create Signal**
    RACReplaySubject *subject = [RACReplaySubject subject];
    // **2. Subscription Signal**
    [subject subscribeNext:^(id _Nullable x) {
    NSLog(@"%@", x);
    }];
    // **3. Send Data**
    // In RACReplaySubject, you can send data before subscribing to a signal;
    // RACSubject does not
    [subject sendNext:@"replay"];

Be careful:
1. RACSubscriber: Represents the subscriber used to signal. This is a protocol, not a class, that must be complied with and implemented to become a subscriber.create a signal with a subscriber to help it send data.
2. RACDisposable: Used to unsubscribe or clean up resources, which is triggered automatically when the signal is sent or when an error is sent.
: If you do not want to listen to a signal, you can actively unsubscribe from it.
3. RACSubject: A signal provider who can act as both a signal and send a signal.(can replace agent)
4. RACReplaySubjet: Repeat the signal class (RACSubject subclass).
: The difference between RACReplaySubject and RACSubject is that RACReplaySubject can send a signal before subscribing to it; RACSubject cannot.
: If a signal is subscribed to once, it is necessary to repeat the previous value and use the repeat to provide the signal class.
: You can set the number of capacities to limit the number of values cached, that is, only the latest values are cached.

Replace proxy implementation with RACSubject
  1. WDYRedView.h
    @interface WDYRedView : UIView
    @property (nonatomic, strong) RACSubject *signalSubject;
    @end
  2. WDYRedView.m
    @implementation WDYRedView
    (RACSubject *)signalSubject {
    if (_signalSubject == nil) { _signalSubject = [RACSubject subject];}
    return _signalSubject;
    }
    (void)clickView:(UITapGestureRecognizer *)gesture {
    [self.signalSubject sendNext:@"Clicked View View"];
    }
    (void)layoutSubviews {
    [super layoutSubviews];
    [self setBackgroundColor:[UIColor redColor]];
    [self setUserInteractionEnabled:YES];
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickView:)]];
    }
    @end
  3. Called in Controller (click to post value)
    // RACSubject instead of proxy implementation - > can pass value
    WDYRedView *redView = [[WDYRedView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    [self.view addSubview:redView];
    [redView.signalSubject subscribeNext:^(id _Nullable x) {
    NSLog(@"%@", x);
    }];

18 July 2020, 12:16 | Views: 4719

Add new comment

For adding a comment, please log in
or create account

0 comments