0
    - (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]);
}
4

1 回答 1

0

尝试这个

[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];
            });
      }
   });

或尝试

[self performSelectorOnMainThread:@selector(udpateAfterFetch:) withObject: [json valueForKey:@"Rooms"] waitUntilDone:YES];

-(void)udpateAfterFetch:(NSArray or whatever *) yourObject
{
   _rooms = yourObject;

   [_table reloadData];

   [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}

该表没有被重新加载,因为任何 UI 更新都应该发生在主线程中,而在你的代码行中并没有发生。

于 2013-07-23T06:35:38.200 回答