Some common RACSignal usage and RACSignal usage

Source: Internet
Author: User

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


Before trying this example, add it to the info. plist file of the Demo project.App Transport Security SettingsKey value, and addAllow Arbitrary Loads:YESKey-value pairs are used to enable insecure connections in iOS.



NSURL * url = [NSURL URLWithString: @ ""]; RACSignal * getDataSignal = [NSData usage: url options: Invalid schednal: [RACScheduler mainthreadschedext]; [getDataSignal subscribeNext: ^ (id x) {NSLog (@ "% @", x); // here x is NSData}];


Map Functions

If we useNSDataOfrac_readContentsOfURL:urlThe method is to download an image. We certainly hope that the last output of this Signal isUIImageObject.mapFunction.

mapFunctions are likesignalThe intermediate processor on the pipeline, from heresignalAfter processingsignalContinue transmission. The processing process ismapThe developer determines the function.

OutputNSDataOfsignalConvert to outputUIImage

NSURL * url = [NSURL URLWithString: @ ""]; 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.mergeThe method is as follows:

NSURL * url = [NSURL URLWithString: @ ""]; NSURL * url2 = [NSURL URLWithString: @ ""]; NSURL * url3 = [NSURL URLWithString: @ ""]; 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.


WithRACSome 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: @ ""]; 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.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.