4

The rac_textSignal-Implementation uses "defer" to return a RACSignal on "self". What is the reason for that?

This is the implementation:

- (RACSignal *)rac_textSignal {
    @weakify(self);
    return [[[[[RACSignal
        defer:^{
            @strongify(self);
            return [RACSignal return:self];
        }]
        concat:[self rac_signalForControlEvents:UIControlEventEditingChanged]]
        map:^(UITextField *x) {
            return x.text;
        }]
        takeUntil:self.rac_willDeallocSignal]
        setNameWithFormat:@"%@ -rac_textSignal", [self rac_description]];
}
itinance
  • 11,711
  • 7
  • 58
  • 98

3 Answers3

3

Both the deferred signal and the concatenated signal send UITextFields (effectively self).

The deferred signal makes the returned signal send the currently stored text on subscription, as [self rac_signalForControlEvents:UIControlEventEditingChanged] only sends on changes.

Simon
  • 1,746
  • 1
  • 13
  • 21
1

If you were to omit defer and implement it like:

- (RACSignal *)rac_textSignal {
    @weakify(self);
    return [[[[[RACSignal return:self]
            concat:[self rac_signalForControlEvents:UIControlEventEditingChanged]]
            map:^(UITextField *x) {
                return x.text;
            }]
            takeUntil:self.rac_willDeallocSignal]
            setNameWithFormat:@"%@ -rac_textSignal", [self rac_description]];
 }

I still don't understand RAC memory management perfectly, however, in above implementation your signal will have strong reference to self, won't ever complete and you will end up with zombie UITextField.

Jakub Vano
  • 3,833
  • 15
  • 29
0

I think defer is just to prevent retain cycle

eliudnis
  • 239
  • 3
  • 6