- (void) setRooms:(NSArray *)newRooms
{
NSLog(@"Main thread(cp3)... %d", [rooms count]);
rooms = newRooms;
[table reloadData];
NSLog(@"Main thread(cp4)... %d", [rooms count]);
}
- (void) parseJSONWithURL:(NSURL *)jsonURL
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSLog(@"Main thread(cp1)...%d", [rooms count]);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSLog(@"Background thread(cp1)...%d", [rooms count]);
NSError *error = nil;
// Request the data and store in a string.
NSString *resp = [NSString stringWithContentsOfURL:jsonURL
encoding:NSASCIIStringEncoding
error:&error];
// Convert the String into an NSData object.
NSData *data = [resp dataUsingEncoding:NSASCIIStringEncoding];
// Parse that data object using NSJSONSerialization without options.
NSDictionary *json = [[NSDictionary alloc] init];
json = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
// Return to the main thread to update the UI elements
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"Main thread(cp2)...%d", [rooms count]);
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[self setRooms:[json valueForKey:@"Rooms"]];
});
NSLog(@"Background thread(cp2)...%d", [rooms count]);
});
NSLog(@"Main thread(cp5)...%d", [rooms count]);
}
Asked
Active
Viewed 981 times
0

Satheesh
- 10,998
- 6
- 50
- 93

Anaximandro Furtado
- 29
- 2
-
Why do you have 2 else's? – Abdullah Shafique Jul 23 '13 at 01:55
-
Hi @AbdullahShafique please note that I also have two chained ifs’… the first if checks the link availability and the second checks parse error. The else’s are the corresponding failure blocks – Anaximandro Furtado Jul 23 '13 at 08:22
1 Answers
0
Try this
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSError *error = nil;
// Request the data and store in a string.
NSString *resp = [NSString stringWithContentsOfURL:jsonURL
encoding:NSASCIIStringEncoding
error:&error];
if (error == nil) {
// Convert the String into an NSData object.
NSData *data = [resp dataUsingEncoding:NSASCIIStringEncoding];
// Parse that data object using NSJSONSerialization without options.
NSDictionary *json = [[NSDictionary alloc] init];
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
dispatch_sync(dispatch_get_main_queue(), ^{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
_rooms = [json valueForKey:@"Rooms"];
[_table reloadData];
});
}
});
or try
[self performSelectorOnMainThread:@selector(udpateAfterFetch:) withObject: [json valueForKey:@"Rooms"] waitUntilDone:YES];
-(void)udpateAfterFetch:(NSArray or whatever *) yourObject
{
_rooms = yourObject;
[_table reloadData];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
The table is not being reloaded because any UI update should happen in the main thread and it is not happening so in ur lines of code.

Satheesh
- 10,998
- 6
- 50
- 93
-
Hi @satheeshwaran, thank you for your answer. I tried both ideas and still not working... In my initial code I was doing the UI update by returning to the main thread in a asynchronous way while you suggest doing it in a synchronous way - that doesn't work. I try to use the selector trick with the background scope or with returning to the main thread scope and still with no luck. – Anaximandro Furtado Jul 23 '13 at 08:14
-
By the way this unexpected behaviour is while I’m running it on iOS Simulator. As you know, Apple Developer page has been down, so I’m not able to access the provisioning portal and test it on real device. Not sure if this is only because I’m on the iOS Simulator... – Anaximandro Furtado Jul 23 '13 at 08:29
-
-
again thank you for your time @satheeshwaran, but I’m still stuck… Would it help if told you that I’m calling the function that runs that piece of code from viewDidLoad callback will this helps? – Anaximandro Furtado Jul 23 '13 at 23:13
-
Calling it from viewDidLoad isn't a problem, I suspect two things here the table reload data part and the table datasource part. NSLog the datasource array before reloading the tableview and also keep a break point in tableView's data source methods - numberOfRowsAtIndexPath and cellForRowAtIndexPath an check whether they are being called at least once. – Satheesh Jul 24 '13 at 06:12
-
-
On the edited code above, I was expecting to see NSLog("numberOfRowsInSection: 9”) between the NSLog(“Main thread(cp3)... 0”) and NSLog(“Main thread(cp4)... 9”) that didn’t happen… – Anaximandro Furtado Jul 24 '13 at 08:09
-
-
Which array's count are you returning in numberOfRows method? Are you using the same in cellForRow. Its a dumb question but though some times we do like that. – Satheesh Jul 24 '13 at 09:14
-
The "rooms" is actually a property of the view and this way is alwasy the same variable... – Anaximandro Furtado Jul 24 '13 at 17:36
-
Dear @satheeshwaran, I finally found the issue: I forgot to link the "IBOutlet UITableView *table” to the correspondent storyboard element. Sorry for wasting your time and million of thanks. – Anaximandro Furtado Jul 24 '13 at 22:12
-
@AnaximandroFurtado Ha ha ha, hilarious dude. Its ok we do silly mistakes all the time. – Satheesh Jul 25 '13 at 06:17