-
Notifications
You must be signed in to change notification settings - Fork 0
/
PLConnection.m
72 lines (57 loc) · 1.75 KB
/
PLConnection.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
67
68
69
70
71
72
//
// PLConnection.m
// Plate
//
// Created by emileleon on 12/9/13.
// Copyright (c) 2013 Plate SF. All rights reserved.
//
#import "PLConnection.h"
#import "PLMenuItem.h"
static NSMutableArray *sharedConnectionList = nil;
@implementation PLConnection
@synthesize request, completionBlock, jsonRootObject;
- (id)initWithRequest: (NSURLRequest *)req
{
self = [super init];
if (self) {
[self setRequest:req];
}
return self;
}
- (void) start
{
container = [[NSMutableData alloc]init];
internalConnection = [[NSURLConnection alloc] initWithRequest:[self request] delegate:self startImmediately:YES];
// NSURLSession *session = [NSURLSession sharedSession];
// task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//
// }];
// [task resume];
if (!sharedConnectionList) {
sharedConnectionList = [[NSMutableArray alloc]init];
}
[sharedConnectionList addObject:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[container appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if ([self jsonRootObject]) {
NSDictionary *d = [NSJSONSerialization JSONObjectWithData:container options:0 error:nil];
[[self jsonRootObject] readFromJSONDictionary:d];
}
if ([self completionBlock]) {
[self completionBlock]([self jsonRootObject], nil);
}
[sharedConnectionList removeObject:self];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if ([self completionBlock]) {
[self completionBlock](nil, error);
}
[sharedConnectionList removeObject:self];
}
@end