+(RACSignal *)requestPhotoData
{
NSURLRequest *request = [self popularURLRequest];
return [[NSURLConnection rac_sendAsynchronousRequest:request] reduceEach:^id(NSURLResponse *response, NSData *data){
return data;
}];
}
創(chuàng)建一個URLConnection請求之后返回的數(shù)據(jù)信號。
+(RACSignal *)importPhotos {
return [[[[[self requestPhotoData]
deliverOn:[RACScheduler mainThreadScheduler]]
map:^id(NSData *data) {
id results = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
return [[[results[@"photos"] rac_sequence] map:^id(NSDictionary *photoDictionary) {
FRPPhotoModel *model = [FRPPhotoModel new];
[self configurePhotoModel:model withDictionary:photoDictionary];
[self downloadThumbnailForPhotoModel:model];
return model;
}] array];
}] publish] autoconnect];
}
deliverOn用來操作線程;
rac_sequence用來序列化,通常用在數(shù)組上;
configurePhotoModel:withDictionary用來把NSDictionary轉(zhuǎn)成Model;
array是把序列化重新轉(zhuǎn)成數(shù)組
publish和autoconnect是為了解決多次請求,假如一個信號中發(fā)送請求,那么每次訂閱都會發(fā)送請求,publish保存訂閱到數(shù)組,當(dāng)調(diào)用連接autoconnect,就會調(diào)用所有的訂閱者的sendNext。