Here's what I'm doing: I'm processing a login that requires manipulating the data afterwards then fires off a new view. I want to do an async request since the server isn't always immediately responsive (and I don't want a crash because I held up the main thread with a synchronous connection)
This is what it looks like now:
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *dataReturn, NSData *data, NSError *error)
{
//blah blah working code
//once data is received, it's processed and I want to call a new view
viewController.hidesBottomBarWhenPushed = YES;
viewController.name = returnUserData[0];
viewController.userID = returnUserData[1];
viewController.role = returnUserData[2];
viewController.sID = returnUserData[3];
[self.navigationController pushViewController:viewController animated:YES];
NSLog(@"Pushed new view controller.");//*/
}];//end async request
Now my problem is that it's not actually visually pushing the view. I can see my NSLog responses are working correctly (the new view immediately responds with "Hello, [name]"), but visually nothing is showing up - This is a problem.
That's fine though, I decided to instead separate the code and try to run the view transition on the main thread
This is an adaptation of I've seen posted online:
NSLog(@"init NSURL response");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Begin async request");
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *dataReturn, NSData *data, NSError *error)
{
NSLog(@"inside async");
if (error)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"There was an error communicating with the server. Please try again." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alert show];
[alert release];
checkField.text = @"";
}
else
{
//NSLog(@"raw: %@ - filtered: %@",dataReturn, (NSString *)dataReturn);
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
rawJSONData = [response objectForKey:@"d"];
NSLog(@"Set jsondata");
}
NSLog(@"ended async");
}];//end async request
NSLog(@"Beginning main thread work");
dispatch_sync(dispatch_get_main_queue(), ^{
//not important for the example
So what it's doing is giving me this:
2014-03-28 21:53:37.059 myApp[652:60b] init NSURL response
2014-03-28 21:53:37.059 myApp[652:3507] Begin async request
2014-03-28 21:53:37.059 myApp[652:3507] Beginning main thread work
It's skipping over the embedded async request entirely.
So what I'm left with is two off-the-main-thread-solutions that aren't doing what I want: I want to run NSURLConnection off the main thread, I'm getting back JSON data I need to parse, and I want to wait until I get & parse that data before transitioning views.
Is that I'm trying to do possible? Or are my eyes glazed over and I'm just not seeing something I should be?