Reputation: 1455
After going through the tutorial I am trying my first exercise in Reactive Cocoa going. The goal is to have a button download something from the internet using AFNetworking and these ReactiveCocoa wrappers.
I came up with this:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = nil;
RACSignal *buttonSignal = [self.stepperButton rac_signalForControlEvents:UIControlEventTouchUpInside];
RACSignal *getSignal = [buttonSignal map:^id(UIButton *button) {
return [manager rac_GET:@"https://farm3.staticflickr.com/2815/13668440264_e6403b3100_o_d.jpg" parameters:nil];
}];
RACSignal *latestSignal = [getSignal switchToLatest];
[latestSignal subscribeNext:^(id x) {
NSLog(@"x: %@",x);
}];
This seems to do a couple of things that I want:
But it fails on other things:
x
is always null
.I guess I am missing lots of things here since I am new to Reactive Cocoa but maybe there are people that are willing to give hints to get me going in the right direction?
Are there other approaches that I fail to see?
Upvotes: 1
Views: 314
Reputation: 1455
This seems to work:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = nil;
RACSignal *buttonSignal = [self.stepperButton rac_signalForControlEvents:UIControlEventTouchUpInside];
RACSignal *getSignal = [buttonSignal map:^id(UIButton *button) {
return [[manager rac_GET:@"https://farm3.staticflickr.com/2815/13668440264_e6403b3100_o_d.jpg" parameters:nil] catch:^RACSignal *(NSError *error) {
NSLog(@"catch %@",error);
return [RACSignal empty];
}];
}];
RACSignal *latestSignal = [getSignal switchToLatest];
[latestSignal subscribeNext:^(NSData *data) {
NSLog(@"dowloaded %d bytes",data.length);
}];
Thanks Stackoverflow similar questions! Powerful stuff.
The catch should be on the rac_GET. Before I had been trying to do things with catch but on the buttonSignal
pipe.
And the reason x
was always null
was because I did not have a serializer set up on the manager
.
I thought about deleting this question, but maybe there are people that still have remarks on the solution?
Upvotes: 1