2013-07-23 56 views
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]); 
} 
+0

爲什麼你有2個東西? –

+0

嗨@AbdullahShafique請注意,我也有兩個鏈接的ifs'...第一個如果檢查鏈接可用性和第二個檢查解析錯誤。其他的是相應的失敗塊 –

回答

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; 

} 

表沒有被重新加載,因爲任何用戶界面更新,應在發生主線程,它不會在你的代碼行中發生。

+0

嗨@satheeshwaran,謝謝你的回答。我嘗試了兩個想法,仍然沒有工作......在我最初的代碼中,我是通過以異步方式返回到主線程來進行UI更新的,而您建議以同步方式進行 - 這是行不通的。我嘗試在後臺作用域中使用選擇器技巧,或者返回主線程作用域,但仍然沒有運氣。 –

+0

順便說一句,這個意外的行爲是,當我在iOS模擬器上運行它。如您所知,Apple Developer頁面已關閉,因此我無法訪問配置門戶並在真實設備上進行測試。不知道這是否只是因爲我在iOS模擬器上... –

+0

現在嘗試編輯的部分,這與模擬器無關。 – satheeshwaran

相關問題