summaryrefslogtreecommitdiff
path: root/Juick
diff options
context:
space:
mode:
Diffstat (limited to 'Juick')
-rw-r--r--Juick/API.m71
1 files changed, 40 insertions, 31 deletions
diff --git a/Juick/API.m b/Juick/API.m
index 4f57dc6..a18b9b1 100644
--- a/Juick/API.m
+++ b/Juick/API.m
@@ -10,7 +10,6 @@
#import "API.h"
@interface API ()
-@property(nonatomic, strong) NSOperationQueue *backgroundQueue;
@property(nonatomic, strong) NSURL *baseURL;
@property(nonatomic, strong) NSURLSession *urlSession;
@@ -26,7 +25,6 @@
NSString *baseURLString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"base_url"];
NSLog(@"Initializing with %@ base URL", baseURLString);
self.baseURL = [NSURL URLWithString:baseURLString];
- self.backgroundQueue = [NSOperationQueue new];
self.urlSession = [NSURLSession sharedSession];
}
return self;
@@ -52,7 +50,10 @@
User *me = [User fromJSON:jsonData];
[[NSUserDefaults standardUserDefaults] setValue:me.token forKeyPath:@"token"];
self.currentUser = me;
- completed(YES);
+ [[NSOperationQueue mainQueue] addOperationWithBlock:^{
+ completed(YES);
+ }];
+
}
}
completed(NO);
@@ -115,13 +116,11 @@
}
-(void) postPMToUser:(NSString *)uname text:(NSString *)text result:(void (^)(NSError *))callback {
- [self.backgroundQueue addOperationWithBlock:^{
- [self post:@"pm" params:@{
- @"uname": uname,
- @"body": text
- } callback:^(NSDictionary *response, NSError *err) {
- callback(err);
- }];
+ [self post:@"pm" params:@{
+ @"uname": uname,
+ @"body": text
+ } callback:^(NSDictionary *response, NSError *err) {
+ callback(err);
}];
}
@@ -177,7 +176,9 @@
NSDictionary *token = [registrationData toJSON];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@[token] options:kNilOptions error:&error];
[self fetchDataWithURL:notificationsUrl data:jsonData boundary:nil authorizationHeader:self.authorizationHeader callback:^(NSData *response, NSError *err) {
- callback(!err);
+ [[NSOperationQueue mainQueue] addOperationWithBlock:^{
+ callback(!err);
+ }];
}];
}
@@ -201,9 +202,7 @@
NSURLSessionDataTask *task = [self.urlSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
- [[NSOperationQueue mainQueue] addOperationWithBlock:^{
- callback(nil, error);
- }];
+ callback(nil, error);
} else {
NSInteger statusCode = ((NSHTTPURLResponse *)response).statusCode;
if (!response || statusCode != 200) {
@@ -211,19 +210,13 @@
NSError *err = [NSError errorWithDomain:@"JuickErrorDomain"
code:statusCode
userInfo:@{@"url": url.absoluteString}];
- [[NSOperationQueue mainQueue] addOperationWithBlock:^{
- callback(nil, err);
- }];
+ callback(nil, err);
} else {
- [[NSOperationQueue mainQueue] addOperationWithBlock:^{
- callback(data, error);
- }];
+ callback(data, error);
}
}
}];
- [self.backgroundQueue addOperationWithBlock:^{
- [task resume];
- }];
+ [task resume];
}
-(NSURL *) url:(NSURL *)url withParameters:(NSDictionary *)params {
@@ -253,14 +246,20 @@
NSURL *requestUrl = params ? [self url:url withParameters:params] : url;
[self fetchDataWithURL:requestUrl data:nil boundary:nil authorizationHeader:self.authorizationHeader callback:^(NSData *data, NSError *err) {
if (err) {
- callback(nil, err);
+ [[NSOperationQueue mainQueue] addOperationWithBlock:^{
+ callback(nil, err);
+ }];
} else {
NSError *jsonError;
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (jsonError) {
- callback(nil, jsonError);
+ [[NSOperationQueue mainQueue] addOperationWithBlock:^{
+ callback(nil, jsonError);
+ }];
} else {
- callback(jsonData, nil);
+ [[NSOperationQueue mainQueue] addOperationWithBlock:^{
+ callback(jsonData, nil);
+ }];
}
}
}];
@@ -275,12 +274,18 @@
NSError *jsonError;
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (jsonError) {
- callback(nil, jsonError);
+ [[NSOperationQueue mainQueue] addOperationWithBlock:^{
+ callback(nil, jsonError);
+ }];
} else {
- callback(jsonData, nil);
+ [[NSOperationQueue mainQueue] addOperationWithBlock:^{
+ callback(jsonData, nil);
+ }];
}
} else {
- callback(nil, err);
+ [[NSOperationQueue mainQueue] addOperationWithBlock:^{
+ callback(nil, err);
+ }];
}
}];
}
@@ -288,9 +293,13 @@
-(void) fetchImageWithURL:(NSURL *) url callback:(void(^)(NSData *))callback {
[self fetchDataWithURL:url data:nil boundary:nil authorizationHeader:self.authorizationHeader callback:^(NSData *data, NSError *err) {
if (err) {
- callback(nil);
+ [[NSOperationQueue mainQueue] addOperationWithBlock:^{
+ callback(nil);
+ }];
} else {
- callback(data);
+ [[NSOperationQueue mainQueue] addOperationWithBlock:^{
+ callback(data);
+ }];
}
}];
}