Some common RACSignal usage and RACSignal usage
NSData + RACSupport. h @ interface NSData (RACSupport) // Read the data at the URL using-[NSData initWithContentsOfURL: options: error:]. // Sends the data or the error. // The semaphore that returns a URL asynchronous request // scheduler cannot be blank + (RACSignal *) rac_readContentsOfURL :( NSURL *) URL options :( NSDataReadingOptions) options schedurl :( RACScheduler *) scheduler; @ end
NSData
Before trying this example, add it to the info. plist file of the Demo project.App Transport Security Settings
Key value, and addAllow Arbitrary Loads
:YES
Key-value pairs are used to enable insecure connections in iOS.
Yes
NSURL * url = [NSURL URLWithString: @ "http://www.jianshu.com"]; RACSignal * getDataSignal = [NSData usage: url options: Invalid schednal: [RACScheduler mainthreadschedext]; [getDataSignal subscribeNext: ^ (id x) {NSLog (@ "% @", x); // here x is NSData}];
Map Functions
If we useNSData
Ofrac_readContentsOfURL:url
The method is to download an image. We certainly hope that the last output of this Signal isUIImage
Object.map
Function.
map
Functions are likesignal
The intermediate processor on the pipeline, from heresignal
After processingsignal
Continue transmission. The processing process ismap
The developer determines the function.
OutputNSData
Ofsignal
Convert to outputUIImage
NSURL * url = [NSURL URLWithString: @ "http://img1.gtimg.com/gamezone/pics/24159/24159840.jpg"]; RACSignal * getDataSignal = [NSData rac_readContentsOfURL: url options: NSDataReadingUncached scheduler: [racschedmainmainthreadscheduler]; // map function to convert RACSignal * getImageSignal = [getDataSignal map: ^ id (id value) {if (value) {return [UIImage imageWithData: value];} return nil ;}]; [getImageSignal subscribeNext: ^ (id x) {NSLog (@ "% @", x) ;}];
Merge Method
Next, we propose a new requirement, request three different images at the same time, and combine the signals they send into a semaphore. Semaphores are used here.merge
The method is as follows:
NSURL * url = [NSURL URLWithString: @ "http://img1.gtimg.com/gamezone/pics/24159/24159840.jpg"]; NSURL * url2 = [NSURL URLWithString: @ "http://i3.hoopchina.com.cn/blogfile/201306/29/137247593017986.jpg"]; NSURL * url3 = [NSURL URLWithString: @ "http://img.youxile.com/pic/1301/25170237170.jpg"]; RACSignal * getImageSignal1 = [[NSData rac_readContentsOfURL: url options: NSDataReadingUncached schedurl: [RACScheduler mainThreadScheduler] map: ^ id (id value) {if) {return [UIImage imageWithData: value] ;}return nil ;}]; RACSignal * getImageSignal2 = [[NSData rac_readContentsOfURL: url2 options: Invalid schedurl: [racschedmainmainthreadscheduler] map: ^ id (id value) {if (value) {return [UIImage imageWithData: value];} return nil ;}]; RACSignal * getImageSignal3 = [[NSData rac_readContentsOfURL: url3 options: NSDataReadingUncached scheduler: [RACScheduler mainThreadScheduler] map: ^ id (id value) {if (value) {return [UIImage imageWithData: value];} return nil;}]; // merge RACSignal * mergeSignal = [RACSignal merge: @ [getImageSignal1, getImageSignal2, signature]; [mergeSignal subscribeNext: ^ (id x) {NSLog (@ "% @", x) ;}]; ''' output: 10:19:08. 776 Fahu [1354: 39991] <UIImage: 0x7f9ce2ac3730>, {450,600} 10:19:08. 891 Fahu [1354: 39991] <UIImage: 0x7f9ce2b02600>, {500,687} 10:19:09. 098 Fahu [1354: 39991] <UIImage: 0x7f9ce29a4500 >,{ 500,346}
### When the filter function network cannot be connected, if any of the three images fails, we do not want 'signal 'to send the 'nil' value, you can use the filter function to filter 'signal. Slightly modify the above 'mergesignal ':
RACSignal* mergeSignal = [[RACSignal merge:@[getImageSignal1,getImageSignal2,getImageSignal3]] filter:^BOOL(id value) {return @(!!value);}];`
Note that the filter function returns the bool value of the NSNumber type, YES is passed, and NO is passed.
NSURLConnection
WithRAC
Some simple network requests do not need the AF framework.
NSURLConnection + RACSupport. h
+ (RACSignal *) rac_sendAsynchronousRequest :( NSURLRequest *) request; ''' for example, request the image NSURL * url = [NSURL URLWithString: @ "http://img1.gtimg.com/gamezone/pics/24159/24159840.jpg"]; NSURLRequest * request = [NSURLRequest requestWithURL: url]; RACSignal * connectionSignal = [NSURLConnection failed: request]; [connectionSignal subscribeNext: ^ (id x) {NSLog (@ "% @", x) ;}]; 'output <RACTuple: 0x7f826c07 1c00>... omit ''' note that the semaphores sent by this 'signal 'are a 'ractuple' object. ### The RACTuple 'ractuple' (tuples) class is a class specifically used by 'rac 'to return multiple returned values. This design is similar to the tuples in swift. In the above example, 'ractuple' has two return values. 'x [0] 'is the header information of http response, and 'x [1]' is the data returned by the request, is an 'nsdata' object. The reading method of the tuples is the same as that of the array.