hit counter

Timeline

My development logbook

Using NSURLConnection to Read Json Data

My code is not good at all for the following reasons:

  • The NSURLConnection retrieves data synchronously

  • The decoding of JSON is not neat. A strong code smell

Synchronous network call

Next step: rewrite the below code to asynchronous mode.

In the viewDidLoad method of my ViewController, I called the following method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-(BOOL) readJSON {

    NSError *error;
    NSURLResponse *response;

    receivedData = [NSMutableData data];

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:_server_url]];
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection) {
        NSData *result = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
        NSDictionary* json = [result objectFromJSONDataWithParseOptions:(JKParseOptionNone)];
        [self convertJsonIntoMyData:json];
        return TRUE;
    } else {
        return FALSE;
    }
}

Decoding of JSON data

I used a lot of enumerators to walk through dictionaries and arrays at different level. It makes future changes to the JSON schema or objective-c object model difficult.