1

I am having a problem storing JSON data into an Array of individual objects. It seems like the problem is in the execution of dispatch_asynch which is handling the JSON request. WHen I create a breakpoint before the method and than step through the application it seems to just fall through the block sent into dispatch_async.

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(queue, ^{ NSError *error = nil; NSURL *url = [NSURL URLWithString:@"http://sleepy-dusk-3603.herokuapp.com/companies.json"]; NSString *json = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error]; NSLog(@"\nJSON: %@ \n Error: %@", json, error); if(!error) { NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding]; NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error]; NSArray *tempArray = [NSArray arrayWithObjects:@"Call Support Desk", @"Call Genius Bar", nil]; for (NSString *name in [jsonDict valueForKeyPath:@"name"]) { NSString *tempString = [[NSString alloc] initWithString:name]; Company *company = [[Company alloc] initWithName:tempString available_actions:tempArray]; [self addCompany:company]; 

I truly appreciate everyone's help and support with this issue.

8
  • Did you put the breakpoint inside the block? Or the dispatch_async call? Commented Mar 5, 2013 at 15:57
  • When I put the breakpoint inside the block, everything seems to work fine (except my UITableView is not being fed the data for some reason). When I put the breakpoint on the original method that calls generate_default_data -> which in turn calls the dispatch_async call, things to not work. Commented Mar 5, 2013 at 16:07
  • Basically I am trying to figure out why the length of an array is 2 within the dispatch_async block, which it is suppose to be...and why it is 0 outside the block. Commented Mar 5, 2013 at 16:12
  • What array are you referring to? Also test if (json != nil), not if (!error). Also why are you using NSASCIIStringEncoding? Are you sure it's not UTF-8? Commented Mar 5, 2013 at 16:28
  • 3
    Also I believe what you are experiencing with the "block falling through" is correct behaviour. If you think about it, you have scheduled the block to be executed on a different thread to the current thread and so the debugger won't step through it (it won't change to a different thread unless you tell it to). Commented Mar 5, 2013 at 16:35

2 Answers 2

1
 if(!error) { ... } else { NSLog(@"%@",[error localizedDescription]); } 

Log the error and find what is happening

this code i tried and is working very well

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(queue, ^{ NSError *error = nil; NSURL *url = [NSURL URLWithString:@"http://sleepy-dusk-3603.herokuapp.com/companies.json"]; NSString *json = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error]; NSLog(@"\nJSON: %@ \n Error: %@", json, error); if(!error) { NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding]; NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error]; NSArray *tempArray = [NSArray arrayWithObjects:@"Call Support Desk", @"Call Genius Bar", nil]; for (NSString *name in [jsonDict valueForKeyPath:@"name"]) { NSString *tempString = [[NSString alloc] initWithString:name]; NSLog(@"%@",tempString); } } }); 

Response

2013-03-05 22:02:44.312 newTrial[4711:12303] JSON: [{"created_at":"2013-03-04T00:09:06Z","id":1,"name":"Apple","updated_at":"2013-03-04T00:09:06Z","actions":[{"created_at":"2013-03-04T00:09:07Z","id":1,"name":"Call Support Desk","updated_at":"2013-03-04T00:09:07Z"},{"created_at":"2013-03-04T00:09:07Z","id":2,"name":"Call Genius Bar","updated_at":"2013-03-04T00:09:07Z"}]},{"created_at":"2013-03-04T02:01:49Z","id":2,"name":"Comcast","updated_at":"2013-03-04T02:01:49Z","actions":[{"created_at":"2013-03-04T02:01:49Z","id":3,"name":"Account Services","updated_at":"2013-03-04T02:01:49Z"}]}] Error: (null) 2013-03-05 22:02:51.766 newTrial[4711:12303] Apple 

So my assumption of the problem is at where you store the value to the array .Check it is properly initialised problem may be here

Company *company = [[Company alloc] initWithName:tempString available_actions:tempArray]; [self addCompany:company]; 
Sign up to request clarification or add additional context in comments.

3 Comments

It seems to never even execute the (!error) block either since it skips over the entire block sent to dispatch_queue
Julian25 - I think you're misunderstanding the way GCD works in the debugger. If you step over or into the dispatch_async call it won't call the block immediately. The block won't be called until you continue
That is actually quite helpful. I guess I did misunderstand how the GCD worked in the debugger. In regards to other answers provided...the root problem I am having is discussed here: stackoverflow.com/questions/15214830/…
0

it works for me like a charm:

dispatch_queue_t _queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(_queue, ^{ NSError *_error = nil; NSURL *_url = [NSURL URLWithString:@"http://sleepy-dusk-3603.herokuapp.com/companies.json"]; NSData *_json = [NSData dataWithContentsOfURL:_url]; if (_json) { id _response = [NSJSONSerialization JSONObjectWithData:_json options:kNilOptions error:&_error]; if (!_error) { // do whatever you want to do here... } else { NSLog(@"%@", _response); } } }); 

the response is:

( { actions = ( { "created_at" = "2013-03-04T00:09:07Z"; id = 1; name = "Call Support Desk"; "updated_at" = "2013-03-04T00:09:07Z"; }, { "created_at" = "2013-03-04T00:09:07Z"; id = 2; name = "Call Genius Bar"; "updated_at" = "2013-03-04T00:09:07Z"; } ); "created_at" = "2013-03-04T00:09:06Z"; id = 1; name = Apple; "updated_at" = "2013-03-04T00:09:06Z"; }, { actions = ( { "created_at" = "2013-03-04T02:01:49Z"; id = 3; name = "Account Services"; "updated_at" = "2013-03-04T02:01:49Z"; } ); "created_at" = "2013-03-04T02:01:49Z"; id = 2; name = Comcast; "updated_at" = "2013-03-04T02:01:49Z"; } ) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.