summaryrefslogtreecommitdiff
path: root/Juick/APIClient.m
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2017-12-07 00:18:52 +0300
committerGravatar Vitaly Takmazov2017-12-07 00:18:52 +0300
commitd856b364dea82f7b89e2040075ee32d988707edb (patch)
tree963308593b095cc069845300f0ed0b59dd7c5e39 /Juick/APIClient.m
parent48234265991dd8ba8e26bd1fa7ea296a9f192ce9 (diff)
refactor
Diffstat (limited to 'Juick/APIClient.m')
-rw-r--r--Juick/APIClient.m28
1 files changed, 18 insertions, 10 deletions
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<AFMultipartFormData> _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);
+ }];
}];
}];
}