Skip to content

Instantly share code, notes, and snippets.

@LawrenceHan
Last active November 18, 2015 06:32
Show Gist options
  • Save LawrenceHan/7d63855f9edd27da6c6e to your computer and use it in GitHub Desktop.
Save LawrenceHan/7d63855f9edd27da6c6e to your computer and use it in GitHub Desktop.
RAC_warpping_AFNetworking
- (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