AFHTTPSessionManager usage via ReactiveCocoa (ReactiveCocoa + AFNetworking)
Inspired by AFNetworking-RACExtensions
It takes functionallity from this and change it code to make more readable and open for change. Simple and helpful, enjoy.
Using AFNetworking (3.1.0) ReactiveCocoa (2.5)
Normally, we create a network layer manager and configure it to use AFNetworking :
- (instancetype) init
{
self = [super init];
NSURL *baseURL = [NSURL URLWithString:kBaseURL];
_sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
_sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
return self;
}
Nothing specific. Next step is to have your net methods in NetworkManager.h Just like this:
@interface NetworkManager : NSObject
+ (instancetype)sharedManager;
- (RACSignal *)postAdWithTitle:(NSString *)title
description:(NSString *)description
cityId:(NSNumber *)cityId
cost:(NSNumber *)cost
currencyId:(NSNumber *)currencyId
imageIds:(NSArray *)imageIds;
@end
Then you go to implement them (in NetworkManager.m) :
@interface NetworkManager ()
@property (strong, nonatomic) AFHTTPSessionManager *sessionManager;
@end
static NSString * const kBaseURL = @"http://<#your-api-base-url#>";
static NSString * const kPostNewAd = @"advertisement/postAd";
@implementation NetworkManager
#pragma mark - Initialization
+ (instancetype)sharedManager
{
static NetworkManager *sharedManager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedManager = [self new];
});
return sharedManager;
}
- (instancetype) init
{
self = [super init];
NSURL *baseURL = [NSURL URLWithString:kBaseURL];
_sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
_sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
return self;
}
#pragma mark - Adverts
- (RACSignal *)postAdWithTitle:(NSString *)title
description:(NSString *)description
cityId:(NSNumber *)cityId
cost:(NSNumber *)cost
currencyId:(NSNumber *)currencyId
years:(NSNumber *)years
imageIds:(NSArray *)imageIds
{
NSDictionary *parameters = @{@"title" : title,
<#all other parameters#>
};
NSLog(@"NetworkManager post ad with parameters : %@",parameters);
return [_sessionManager rac_requestWithMethod:POST
URL:kPostNewAd
parameters:parameters];
}
@end
So, next step is to use it:
[[NetworkManager sharedManager] postAdWithTitle:_title
description:_descr
cityId:_city.id
cost:_cost
currencyId:_currencyId
imageIds:imageIds]
subscribeNext:^(id x) {
NSLog(@"testPostAd : %@",x);
}
error:^(NSError *error) {
NSLog(@"Error testPostAd : %@", error);
}];
You can find real usage example in PixabayTest app, it's really simple, but hope useful.
Just go into /PixabayTest/Model/Network/PBApiManager
You also can download and play with it
Aleksandr Zaporozhchenko [github] [gmail] [fb] [in]
AFHTTPSessionManager-RACExtensions is released under the MIT license. See LICENSE for details.