From d856b364dea82f7b89e2040075ee32d988707edb Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 7 Dec 2017 00:18:52 +0300 Subject: refactor --- Juick/APIClient.m | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'Juick/APIClient.m') diff --git a/Juick/APIClient.m b/Juick/APIClient.m index bcc4eec..abefa40 100644 --- a/Juick/APIClient.m +++ b/Juick/APIClient.m @@ -9,6 +9,10 @@ #import "PDKeychainBindings.h" #import "Message.h" +@interface APIClient() +@property(nonatomic, strong) NSOperationQueue *backgroundQueue; +@end + @implementation APIClient +(APIClient *) sharedClient { @@ -24,38 +28,38 @@ if (self = [super init]) { self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.juick.com"]]; self.manager.requestSerializer = [AFJSONRequestSerializer new]; + self.backgroundQueue = [NSOperationQueue new]; } return self; } -(void) pullNextFromPath:(NSString *)path params:(NSDictionary *) params callback:(void(^)(NSArray *, NSError *))callback { AFHTTPSessionManager *manager = [APIClient sharedClient].manager; - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + [self.backgroundQueue addOperationWithBlock:^{ [manager GET:path parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSMutableArray *messages = [NSMutableArray new]; [((NSArray *)responseObject) enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [messages addObject:[Message yy_modelWithJSON:obj]]; }]; - dispatch_async(dispatch_get_main_queue(), ^{ + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ callback(messages, nil); - }); + }]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"REST Error: %@", error); NSInteger statusCode = ((NSHTTPURLResponse *)task.response).statusCode; if (statusCode == 401) { - dispatch_async(dispatch_get_main_queue(), ^{ + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ callback(nil, [[NSError alloc] initWithDomain:@"JuickErrorDomain" code:401 userInfo:nil]); - }); + }]; } }]; - }); + }]; } -(void) postReplyToThread:(NSNumber *)mid inReplyTo:(NSNumber *)rid text:(NSString *)text result:(void(^)(Message *, NSError *))callback { AFHTTPSessionManager *manager = [APIClient sharedClient].manager; - NSOperationQueue *operationQueue = [NSOperationQueue new]; - [operationQueue addOperationWithBlock:^{ + [self.backgroundQueue addOperationWithBlock:^{ [manager POST:@"/comment" parameters:nil constructingBodyWithBlock:^(id _Nonnull formData) { [formData appendPartWithFormData:[[mid stringValue] dataUsingEncoding:NSUTF8StringEncoding] name:@"mid"]; [formData appendPartWithFormData:[[rid stringValue] dataUsingEncoding:NSUTF8StringEncoding] name:@"rid"]; @@ -63,10 +67,14 @@ } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"Success!"); Message *reply = [Message yy_modelWithJSON:responseObject]; - callback(reply, nil); + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + callback(reply, nil); + }]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"Error: %@", [error localizedDescription]); - callback(nil, error); + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + callback(nil, error); + }]; }]; }]; } -- cgit v1.2.3