2014-07-17 5 views
0

내 응용 프로그램을 테스트하기 위해 가짜 값을 그리는 JSON 파일을 만들었지 만 테이블의 뷰에서 사용자 데이터를 제외하고 싶습니다. JSON 파일이 UITableView까지로드되지 않습니다.

//create a new JSONLoader with a local file from URL 
JSONLoader *jsonLoader = [[JSONLoader alloc] init]; 
NSURL *url = [[NSBundle mainBundle] URLForResource:@"chatters" withExtension:@"json"]; 
//load the data on a background queue 
//use for when connecting a real URL 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
                     _localChatters = [jsonLoader chattersFromJSONFile:url]; 
                     //push data on main thread (reload table view once JSON has arrived) 
                      //[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; 
                     }); 

가 그럼 난 아무런 문제가있는있는 tableView에로드 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{ 
static NSString *CellIdentifier = @"PopulationCell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil){ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
Chatter *chatter = [_localChatters objectAtIndex:indexPath.row]; 

NSData *fbImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: chatter.url]]; 
UIImage *profilePicture = [UIImage imageWithData:fbImageData]; 
cell.imageView.image =profilePicture; 

[cell.textLabel setAdjustsFontSizeToFitWidth: NO]; 

cell.textLabel.text = [NSString stringWithFormat:@"%@ joined at %@",chatter.name,chatter.joined]; 

cell.textLabel.textColor = [UIColor grayColor]; 

return cell; 
} 

그러나이뿐만 아니라 사용자 정보를 포함하는 뭔가 우리되는이 파일을로드하기 위해이 코드를 사용했다 원하지 않아. 이 문제를 해결하기 위해 위해 나는 이론을 제외한 데이터 만에 두 번째 가변 배열을 만들 것입니다 별도의 방법을 생성 한 사용자의 :

- (void)getData{ 
NSLog(@"%@",_localChatters); 

NSMutableArray *newArray = [[NSMutableArray alloc] init]; 

for (NSInteger i = 0; i<[_localChatters count]; i++) { 

    Chatter *newChatter = [_localChatters objectAtIndex:i]; 

    if ([newChatter.facebookID isEqualToString:_loggedInFBID]) { 
    } else { 

     [newArray addObject:newChatter]; 
    }; 
} 
NSLog(@"%@",newArray); 

}이 메소드가 호출되는

그러나 보기 (null)와 같은

[self getData] 

_localChatters의 NSLogs와 부하를하고 난 이후에 newArray이 채워지지와 빈 등 NSLogs() 결코 생각합니다. 그것은 uitableview에 _localChatters를 로그 할 때 null이 아니며로드 될 때 모든 데이터가 거기에 있기 때문에 이상합니다. 나는 배열에 dispatch_async 요청에서 정상적으로로드 될 때 _localChatters가이 메서드에서 null로 읽는 이유를 알 수 없습니다.

편집 : 여기

, 나는 그러나 모든 개체가 동일한, 같은 FB의 ID 및 사진 URL과 같이 개인 정보의 일부를 삭제 한 JSON의 작은 샘플입니다.

  "name": "Johnny", 
      "room": "London", 
      "latitude": 41.414483, 
      "longitude": 2.152579, 
      "message": "I agree, I think I'm going there right now", 
      "timestamp": "9:23 PM", 
      "url": " <<actual FB profile URL>>", 
      "facebookID":"<<personal FB ID >>", 
      "joined":"12:13 AM", 
+1

이 JSON의 모양을 알 수 있습니까? –

+1

참조로 값을 반환하지 않는 경우 접두사 "get"이있는 메서드의 이름을 지정하지 마십시오. – zaph

+0

감사합니다. @Zaph, 저는 테스트를 위해 함께 쳤습니다. 영구적 인 해결책이 될 수는 없었습니다. – canaanmckenzie

답변

2

문제는 멀티 스레딩입니다. _localChatters 배열이 비동기식으로 작성되므로 대부분의 경우 viewDidLoad 이후에 발생합니다. [self getData]dispatch_async 블록으로 이동할 수 있습니다 (테이블보기를 다시로드하기 전과 JSON에서 데이터를 가져온 후).

+0

매력처럼 작동 했으니 까 이것에 익숙하지 않았습니다. 감사합니다. – canaanmckenzie

관련 문제