Last active
November 18, 2015 06:32
-
-
Save LawrenceHan/7d63855f9edd27da6c6e to your computer and use it in GitHub Desktop.
RAC_warpping_AFNetworking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// When view disappeared cancel the request | |
RACSignal *willDisappear = [self rac_signalForSelector:@selector(viewWillDisappear:)]; | |
// load data | |
@weakify(self) | |
[[[[self loadDataSignal] | |
takeUntil:willDisappear] | |
deliverOnMainThread] | |
subscribeNext:^(NSDictionary *responseObject) { | |
@strongify(self); | |
// Do something | |
} error:^(NSError *error) { | |
DDLogDebug(@"An error occurred: %@", error); | |
}]; | |
} | |
- (RACSignal *)loadDataSignal { | |
@weakify(self) | |
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { | |
@strongify(self) | |
AFHTTPRequestOperation *operation = [self.requestManager | |
POST:@"an_url" | |
parameters:@{@"parameter" : @"parameter"} | |
success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) { | |
[subscriber sendNext:responseObject]; | |
[subscriber sendCompleted]; | |
} | |
failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) { | |
[subscriber sendError:error]; | |
}]; | |
return [RACDisposable disposableWithBlock:^{ | |
[operation cancel]; | |
}]; | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment