-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCOJSONManager.m
66 lines (55 loc) · 1.72 KB
/
CCOJSONManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// CCOJSONManager.m
// CCOJSONManagerExample
//
// Created by Chris on 5/14/14.
// Copyright (c) 2014 ChrisCombs. All rights reserved.
//
#import "CCOJSONManager.h"
@interface CCOJSONManager ()
@property (nonatomic, strong) NSOperationQueue *queue;
@property (nonatomic, strong) id storedData;
@property (nonatomic, strong) NSURL *jsonURL;
@end
@implementation CCOJSONManager
+ (CCOJSONManager*)sharedInstance {
static CCOJSONManager *singleton;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singleton = [CCOJSONManager new];
});
return singleton;
}
// Using an operation queue to automatically return the leaderboard data if it exists
- (void)getLeaderboardDataWithCompletionHandler:(void (^)(NSArray *data))completion {
NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{
completion(self.storedData);
}];
[self.queue addOperation:block];
}
- (void)setJSONURL:(NSURL *)url {
self.jsonURL = url;
[self sendRequest];
}
#pragma mark - Private (ish)
-(id)init {
self = [super init];
if (self) {
_queue = [NSOperationQueue new];
[_queue setSuspended:YES];
}
return self;
}
- (void)sendRequest {
NSURLRequest *request = [NSURLRequest requestWithURL:self.jsonURL];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.storedData = JSON;
[self.queue setSuspended:NO];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"%@", error);
}];
[operation start];
}
@end