2012-07-02 1 views
1

iPhone 응용 프로그램을 작성합니다. 이 응용 프로그램에서는 트위터 프레임 워크를 사용합니다. 이 프레임 워크에서, 동기화 해제 된 콜백 함수는 다른 스레드에 있습니다. 내보기 컨트롤러에서 Objective-C의 다른 스레드에서 NSURLConnection에 의한 데이터를 수신하는 방법

,

ViewController.m

[accountStore requestAccessToAccountsWithType:accountType 
         withCompletionHandler:^(BOOL granted, NSError *error) { 
          if (granted) { 
           if (account == nil) { 
            NSArray *accountArray = [accountStore accountsWithAccountType:accountType]; 
            account = [accountArray objectAtIndex:2]; 
           } 

           if (account != nil){ 
            NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/user_timeline.json"]; 
            NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; 
            [params setObject:@"1" forKey:@"count"]; 

            TWRequest *request = [[TWRequest alloc] initWithURL:url 
             parameters:params 
             requestMethod:TWRequestMethodGET]; 
            [request setAccount:account]; 
            [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
             if (responseData) { 
              //Throw response data to other Web API 
              [self otherAPI:responseData]; 
              [[NSRunLoop currentRunLoop] run]; 
             } 
            }]; 

           } 
          } 


         }]; 

그리고이 클래스에서 이러한 방법을 쓰기.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection; 

하지만 다른 API에서 전체 데이터를받을 수 없습니다. 첫 번째 데이터 만받을 수 있습니다. 다중 스레드를 수행 할 때 몇 가지 문제가 있다고 생각합니다. 따라서이 코드에서 무엇이 잘못되었는지 알려주고 싶습니다.

답변

0

문제가있는 것 같습니다. -connection:didReceiveData:이 여러 번 호출되면 전체 메시지를 포함 할 NSMutableData 객체를 빌드해야합니다.

참고 : 한 번에 인스턴스 당 하나의 다운로드에만 작동합니다.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    self.responseData = [[NSMutableData dataWithCapacity:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [self.responseData appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    // self.responseData has all the data. 
} 
+0

해 주셔서 감사합니다. 그러나 나는 이미 그 선택을했습니다. 그 후, 나는이 문제를 해결했다. 나는 일본어를 사용한다. 그래서 - (NSString *) stringByAddingPercentEscapesUsingEncoding 메서드는 필요한 것처럼 보입니다. 전체 데이터를받을 수 있습니다. NSLog는 이스케이프 처리되지 않은 데이터를 표시 할 수 없다고 생각합니다. 고맙습니다. – Lewuathe

관련 문제