1717
1818+ (void)download:(NSString *)urlString withCompletion:(void (^)(NSData *data))completion {
1919 NSAssert(urlString, @"URL must not be nil");
20-
20+
2121 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
22- [NSURLConnection sendAsynchronousRequest:request
23- queue:[NSOperationQueue mainQueue]
22+ [NSURLConnection sendAsynchronousRequest:request
23+ queue:[NSOperationQueue mainQueue]
2424 completionHandler:
2525 ^(NSURLResponse *response, NSData *data, NSError *connectionError) {
2626 if(completion) {
2727 completion(data);
28- }
28+ }
2929 }];
3030}
3131
@@ -43,10 +43,10 @@ Completion blocks?这是另外一个使用Signals的机会。更深入一点来
4343
4444+ (RACSignal *)download:(NSString *)urlString {
4545 NSAssert(urlString , @"URL must not be nil");
46-
46+
4747 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: urlString]];
48-
49- return [[[NSURLConnection rac_sendAsynchronousRequest:request]
48+
49+ return [[[NSURLConnection rac_sendAsynchronousRequest:request]
5050 map:^id (RACTuple *value) {
5151 return [value second];
5252 }] deliverOn:[RACScheduler mainThreadScheduler]];
@@ -76,10 +76,10 @@ RAC(photoModel, thumbnailData) = [self download:photoModel.thumbnailURL];
7676```
7777+ (RACReplaySubject * )fetchPhotoDetails:(FRPPhotoModel * )photoModel {
7878 RACReplaySubject *subject = [RACReplaySubject subject];
79-
79+
8080 NSURLRequest *request = [self photoURLRequest:photoModel];
81- [NSURLConnection sendAsynchronousRequest:request
82- queue:[NSOperationQueue mainQueue]
81+ [NSURLConnection sendAsynchronousRequest:request
82+ queue:[NSOperationQueue mainQueue]
8383 completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
8484 if(data) {
8585 id results = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil][@"photo"];
@@ -90,12 +90,12 @@ RAC(photoModel, thumbnailData) = [self download:photoModel.thumbnailURL];
9090 }
9191 else {
9292 [subject sendError:connectionError];
93- }
93+ }
9494 }];
95-
96-
95+
96+
9797 return subject;
98-
98+
9999}
100100```
101101
@@ -105,13 +105,13 @@ RAC(photoModel, thumbnailData) = [self download:photoModel.thumbnailURL];
105105```Objective-C
106106+ (RACSignal *)fetchPhotoDetails:(FRPPhotoModel *)photoModel {
107107 NSURLRequest *request = [self photoURLRequest:photoModel];
108- return [[[[[[NSURLConnection rac_sendAsynchronousRequest:request]
108+ return [[[[[[NSURLConnection rac_sendAsynchronousRequest:request]
109109 map:^id(RACTuple *value){
110110 return [value second];
111111 }]
112112 deliverOn:[RACScheduler mainThreadScheduler]]
113113 map:^id (NSData *data) {
114- id results = [NSJSONSerialization JSONObjectWithData:data
114+ id results = [NSJSONSerialization JSONObjectWithData:data
115115 options:0 error:nil][@"photo"];
116116 [self configurePhotoModel:photoModel withDictionary:results];
117117 [self downloadFullsizedImageForPhotoModel:photoModel];
@@ -127,7 +127,7 @@ RAC(photoModel, thumbnailData) = [self download:photoModel.thumbnailURL];
127127
128128我们已经知道` deliverOn: ` 是怎样工作的,所以让我们来关注信号链条最末端的信号操作` publish ` . ` publish ` 返回一个` RACMulitcastConnection ` ,当信号连接上时,他将订阅该接收信号。` autoconnect ` 为我们做的是:当它返回的信号被订阅,连接到
129129 该(订阅背后的)信号(underly signal)。
130-
130+
131131 执行获取每一个订阅,在订阅的时候,我们返回的信号将会变“冷”。那是因为我们对底层信号进行多播,网络请求只会执行一次,但是它的结果被多播。这会导致:网络信号将只会被执行一次(当它被订阅时执行),是冷的(直到订阅为止,它不会被执行),甚至可删除的(如果一次性处理订阅的生成)。
132132
133133基本上,我们能保证信号只会被订阅一次,我们不需要回滚(replay).
@@ -145,15 +145,15 @@ reduceEach:^id(NSURLResponse *response, NSData *data) {
145145```
146146+ (RACSignal *)importPhotos {
147147 NSURLRequest *request = [self popularURLRequest];
148-
149- return [[[[[[NSURLConnection rac_sendAsynchronousRequest:request]
148+
149+ return [[[[[[NSURLConnection rac_sendAsynchronousRequest:request]
150150 reduceEach:^id(NSURLResponse *response , NSData *data){
151151 return data;
152- }]
153- deliverOn:[RACScheduler mainThreadScheduler]]
152+ }]
153+ deliverOn:[RACScheduler mainThreadScheduler]]
154154 map:^id (NSData *data) {
155155 id results = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
156- return [[[results[@"photo"] rac_sequence]
156+ return [[[results[@"photo"] rac_sequence]
157157 map:^id (NSDictionary *photoDictionary) {
158158 FRPPhotoModel *model = [FRPPhotoModel new];
159159 [self configurePhotoModel:model withDictionary:photoDictionary];
0 commit comments