( The whole thing about ReactiveCocoa Our project can be in https://github.com/qianhongqiang/QHQReactive download )

ReactiveCocoa I will not talk about the introduction of , Baidu .ReactiveCocoa This framework is very popular at present , But it's not popular . In the final analysis , The cost of learning is too high , Head file , You hardly know how to use it .ReactiveCocoa Internal implementations use a lot of block, stay block In the case of deep nesting , It's also hard to read the source code . I'm going to do a pull out for you , Break down the modules . Because when I started reading , I don't even know how to start . Source code analysis if there are errors , Welcome to correct .

First of all, let me show you a paragraph ReactiveCocoa Code for

```

   RACSignal *signal = [RACSignal createSignal:^(id subscriber) {
     [subscriber sendNext:[NSDate date]];
     [subscriber sendCompleted];
     return  nil;
   }];
   [signal subscribeNext:^(id x) {
     NSLog(@ "next:%@" , x);
   } error:^(NSError *error) {
     NSLog(@ "error:%@" , error);
   } completed:^{
     NSLog(@ "completed" );
   }];
```
The purpose of this code is to create a model , And subscribe to this model . The code is elegant, isn't it ? Behind elegance , It always takes a lot of work to support .
Now I'm going to rewrite RAC, It's just a simple version of , The simple version is to keep the main logic , I've dealt with the details . This section only implements the above functions .
Two classes are implemented here QHQSignal and QHQSubscriber
```

@interface QHQSignal ()

@property (nonatomic, copy) void (^didSubscriber)(id subscriber);

@property (nonatomic, strong) QHQSubscriber *scriber;

@end

@implementation QHQSignal

+(QHQSignal *)createSignal:(void (^)(id subscriber))didSubscriber {

QHQSignal *signal = [[self alloc] init];

signal.didSubscriber = didSubscriber;

return signal;

}

-(void)subscribeNext:(void (^)(id))nextBlock {

QHQSubscriber *scriber = [QHQSubscriber subscriberWithNextBlock:nextBlock];

self.scriber = scriber;

self.didSubscriber(self.scriber);

}

@end

```
```

@interface QHQSubscriber ()

@property (nonatomic, copy) void(^nextBlock)(id x);

@property (nonatomic, copy) void(^completionBlock)(id x);

@property (nonatomic, copy) void(^errorBlock)(id x);

@end

@implementation QHQSubscriber

+(instancetype)subscriberWithNextBlock:(void (^)(id x))nextBlock {

return [self subscriberWithNextBlock:nextBlock completionBlock:nil errorBlock:nil];

}

+(instancetype)subscriberWithNextBlock:(void (^)(id x))nextBlock completionBlock:(void (^)(id x))completionBlock errorBlock:(void (^)(id x))errorBlock {

QHQSubscriber *subscriber = [[QHQSubscriber alloc] init];

subscriber.nextBlock = nextBlock;

subscriber.completionBlock = completionBlock;

subscriber.errorBlock = errorBlock;

return subscriber;

}

-(void)sendNext:(id)next {

if (self.nextBlock) {

self.nextBlock(next);

}

}

@end

```
 
QHQSignal Implements a creation method , It needs to be executed when it is created block Pass in ,block There is one subcriber Parameters , That's the internal subscriber of the signal ( in order to rac The elegance of realization , The first confusing design ), This subscriber keeps the real subscriber , In the process of subscribing to the signal . For example , That's what you told me subcriber When the signal comes , What are we going to do? .
After creating the signal , Once there are subscribers , That is, the signal is subscribeNext( For convenience, I will not consider compelete, And error) When ,rac Internally, a subscriber, And this subscriber Saved your executive block.subscriber Actually, it's the same as getting the authorization .
```

QHQSignal *demoSignal = [QHQSignal createSignal:^(id subscriber) {

[subscriber sendNext:@"1"];

}];

[demoSignal subscribeNext:^(id x) {

NSLog(@"%@",x);

}];

```
Simply realized the above code , Is it much simpler . More than one section , It's easy to vomit , And see this .

ReactiveCocoa Source code splitting and parsing ( One ) More articles about

  1. ReactiveCocoa Source code splitting and parsing ( Two )

    ( The whole thing about ReactiveCocoa The code engineering of can be in https://github.com/qianhongqiang/QHQReactive download ) The main signal mechanism is separated from the above . But in RA ...

  2. ReactiveCocoa Source code splitting and parsing ( Four )

    ( The whole thing about ReactiveCocoa The code engineering of can be in https://github.com/qianhongqiang/QHQReactive download ) The previous chapter briefly explained how to realize the thermal signal . But write like that , ...

  3. ReactiveCocoa Source code splitting and parsing ( 7、 ... and )

    ( The whole thing about ReactiveCocoa The code engineering of can be in https://github.com/qianhongqiang/QHQReactive download ) In this blog , I will put ReactiveCocoa Wipe in ...

  4. ReactiveCocoa Source code splitting and parsing ( 6、 ... and )

    ( The whole thing about ReactiveCocoa The code engineering of can be in https://github.com/qianhongqiang/QHQReactive download ) RAC For elegant signal binding , It can be said that I have tried my best , Not only ...

  5. ReactiveCocoa Source code splitting and parsing ( 5、 ... and )

    ( The whole thing about ReactiveCocoa The code engineering of can be in https://github.com/qianhongqiang/QHQReactive download ) I haven't written for many days , Continue today . Main explanation RAC How to UI ...

  6. ReactiveCocoa Source code splitting and parsing ( 3、 ... and )

    ( The whole thing about ReactiveCocoa The code engineering of can be in https://github.com/qianhongqiang/QHQReactive download ) This chapter mainly discusses the signal “ cold ” And “ heat ” stay RAC The world of ...

  7. mybatis 3.x Source code depth analysis and best practices ( The most complete and original )

    mybatis 3.x Source code depth analysis and best practices 1 Environmental preparation 1.1 mybatis Introduction and framework source code learning objectives 1.2 This series of source code analysis method 1.3 Environment building 1.4 from Hello World Start 2 ...

  8. spring Source depth analysis — IOC And Turn on bean Loading

    summary We have analyzed spring about xml Profile resolution , Assemble the analyzed information into BeanDefinition, And save it and register it with the corresponding BeanDefinitionRegistry in . thus ,Spri ...

  9. Spring Framework spring-web web The source code is completely parsed

    Spring Framework spring-web web The source code is completely parsed spring-web yes Spring webMVC The basis of , from http.remoting.web Three parts , The core is web modular .http The module encapsulates ...

Random recommendation

  1. be based on redis Like function design of

    Preface Like is actually a very interesting feature . There are two basic design ideas , One kind of nature is to use mysql etc. The database is directly stored on the ground , The other is to use the business features of like to throw redis( or memcache) in , And then offline brush back m ...

  2. watch Command real time command execution monitoring

    watch command   watch -d -n 1 'df; ls -FlAt /path' To use this command, you need to replace one of the /path part ,watch It's a real-time monitoring tool ,-d The parameter is highlighted Show changing ...

  3. Use Discuz! Built in parameter defense CC Attack and principle , modify Discuz X Turn on the anti-jamming device CC After the attack , Does not affect the search engine included method

    This part of the work , It used to take too little time . Hope to have a certain effect . http://www.nigesb.com/discuz-cc-attacker-defence.html http://bbs.zb7.co ...

  4. UVa 10602 - Editor Nottoobad

    The main idea of the topic : There's an editor , It has two commands ,“ Repeat the last word ” and “ Delete the previous letter ”, Given a series of strings , Find the minimum number of keystrokes . The first word in the title must be the first word given , So we thought about the word and the first list ...

  5. ecshop Detailed installation steps for

    Find a... On the Internet ecshop package , Then download , decompression , After decompression ecshop It can't be used directly , You have to change the permissions of several directories to use . ecshop Put it on www Under the table of contents , In this way, you can visit directly http://localhost/e ...

  6. robotframework Environmental construction

    Error reported at startup , The environment variables are not configured properly error : command: pybot.bat --argumentfile c:\users\keikei\appdata\local\temp\RIDEam ...

  7. Batch command call WINRAR Compress files

    command : winrar a  Parameters   Compressed file Compress source files Such as compression apk file , The code is as follows echo apk Files cannot be downloaded directly , So compression apk become rar To download d: cd D:\Program Files\W ...

  8. my 2018:OCR、 Internships and autumn recruitment

    Time really flies , It seems that I was still immersed in the success of the postgraduate entrance exam yesterday , Today is about to begin 2019 year , About to graduate with a master's degree . Old rules , On the last day of each year, I summarize this year and look forward to next year . Looking back 2018, There's so much to experience , My vision has been broadened a lot , so to speak , ...

  9. 6. analysis request_irq and free_irq Function to register the logout interrupt ( Detailed explanation )

    The last section talked about how to implement a run interrupt , These are all done by the system , When we want to write our own interrupt handler , To execute your own code , You need to write irq_desc->action->handler, And then through request_irq ...

  10. GitHub Some of the tools for sorting out 【 Reprint 】

    Technology sites Hacker News: Great link aggregation site for programming Programming reddit: ditto MSDN: Microsoft's official technology is concentrated in , Mainly the document class infoq: Enterprise application , Focus on software development ...