2011-05-01 2 views
2

NSOperationQueue 및 NSOperation을 사용하여 트위터에 대한 요청을 수행하고 있습니다. 그러나이 요청을 수행하고 결과를받을 때 모든 결과가 다운로드 될 때까지 응용 프로그램이 몇 초 동결됩니다.NSOperation 및 NSOperationQueue 고정 응용 프로그램

이 문제를 방지 할 수있는 방법이 있습니까?

편집 ----

내 코드는 다음과 같습니다

- (void)getPublicTimeline { 
    TwitterRequest *request = [[TwitterRequest alloc] initWithRequestType:publicTimelineRequest andManager:self]; 
    [queue addOperation:request]; 
    [queueArray addObject:request]; 
    [request release]; 
} 

TwitterRequest (NSOperation)

- (id)initWithRequestType:(requestTypeEnum)type andManager:(TwitterManager *)managr { 
self = [super init]; 
if (self) { 
    [self setRequestType:type]; 
    [self setManager:managr]; 
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 
    [self performSelector:@selector(startWithRequest)]; 
} 
return self; 

}

- (void)startWithRequest { 
if (requestType == statusRequest) { 
    [self performSelector:@selector(doStatusRequest)]; 
} 
    else if (requestType == privateMessageRequest) { 
    [self performSelector:@selector(doPrivateMessageRequest)]; 
} 
else if (requestType == sentPrivateMessageRequest) { 
    [self performSelector:@selector(doSentPrivateMessageRequest)]; 
} 
else if (requestType == allFollowersRequest) { 
    [self performSelector:@selector(allFollowersRequest)]; 
} 
else if (requestType == publicTimelineRequest) { 
    [self performSelector:@selector(doPublicTimelineRequest)]; 
} 

}

- (void)doPublicTimelineRequest { 
    connectionId = [manager.engine getPublicTimeline]; 
} 

트위터 관리자 :

- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier { 
    TwitterRequest *lastObj = (TwitterRequest *)[queueArray lastObject]; 
    if (lastObj.requestType == statusRequest) { 
     if ([[self delegate] conformsToProtocol:@protocol(TwitterManagerDelegate)] && [[self delegate] respondsToSelector:@selector(manager:receivedStatuses:)]) { 
      [[self delegate] manager:self receivedStatuses:statuses]; 
     } 
    } 
    else if (lastObj.requestType == publicTimelineRequest) { 
     NSMutableArray *tweets = [[NSMutableArray alloc] init]; 
     for (int i = 0; i < [statuses count]; i++) { 
      TimelineTweet *tt = [[TimelineTweet alloc] init]; 
      User *user = [[User alloc] init]; 

      tt.createdAt = [[statuses objectAtIndex:i] objectForKey:@"created_at"]; 
      if ([[[statuses objectAtIndex:i] objectForKey:@"favorited"] isEqualToString:@"false"]) 
       tt.isFavorited = FALSE; 
      else 
       tt.isFavorited = TRUE; 
      if ([[[statuses objectAtIndex:i] objectForKey:@"retweeted"] isEqualToString:@"false"]) 
       tt.retweeted = FALSE; 
      else 
       tt.retweeted = TRUE; 
      tt.retweetCount = [[statuses objectAtIndex:i] objectForKey:@"retweet_count"]; 
      tt.source = [[statuses objectAtIndex:i] objectForKey:@"source"]; 
      tt.tweet = [[statuses objectAtIndex:i] objectForKey:@"text"]; 
      tt.tweetID = [[statuses objectAtIndex:i] objectForKey:@"id"]; 
      user.createdAt = [[[statuses objectAtIndex:i] objectForKey:@"user"] objectForKey:@"created_at"]; 
      user.description = [[[statuses objectAtIndex:i] objectForKey:@"user"] objectForKey:@"description"]; 
      user.favouritesCount = [[[statuses objectAtIndex:i] objectForKey:@"user"] objectForKey:@"favourites_count"]; 
      user.friendsCount = [[[statuses objectAtIndex:i] objectForKey:@"user"] objectForKey:@"friends_count"]; 
      if ([[[[statuses objectAtIndex:i] objectForKey:@"user"] objectForKey:@"verified"] isEqualToString:@"false"]) 
       user.isVerified = FALSE; 
      else 
       user.isVerified = TRUE; 
      user.lang = [[[statuses objectAtIndex:i] objectForKey:@"user"] objectForKey:@"lang"]; 
      user.listCount = [[[statuses objectAtIndex:i] objectForKey:@"user"] objectForKey:@"listed_count"]; 
      user.location = [[[statuses objectAtIndex:i] objectForKey:@"user"] objectForKey:@"location"]; 
      user.name = [[[statuses objectAtIndex:i] objectForKey:@"user"] objectForKey:@"name"]; 
      user.profileBackgroundColor = [[[statuses objectAtIndex:i] objectForKey:@"user"] objectForKey:@"profile_background_color"]; 
      user.profileBackgroundImageURL = [[[statuses objectAtIndex:i] objectForKey:@"user"] objectForKey:@"profile_background_image_url"]; 
      user.profileImageURL = [[[statuses objectAtIndex:i] objectForKey:@"user"] objectForKey:@"profile_image_url"]; 
      user.screenName = [[[statuses objectAtIndex:i] objectForKey:@"user"] objectForKey:@"screen_name"]; 
      user.tweetCount = [[[statuses objectAtIndex:i] objectForKey:@"user"] objectForKey:@"statuses_count"]; 
      user.url = [[[statuses objectAtIndex:i] objectForKey:@"user"] objectForKey:@"url"]; 
      user.userID = [[[statuses objectAtIndex:i] objectForKey:@"user"] objectForKey:@"id"]; 
      user.profileImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[user profileImageURL]]]; 
      tt.user = user; 
      [user release]; 
      [tweets addObject:tt]; 
      if ([[self delegate] conformsToProtocol:@protocol(TwitterManagerDelegate)] && [[self delegate] respondsToSelector:@selector(manager:didReceivePublicTimelineObject:)]) { 
       [[self delegate] manager:self didReceivePublicTimelineObject:tt]; 
      } 
      [tt release]; 
     } 
     if ([[self delegate] conformsToProtocol:@protocol(TwitterManagerDelegate)] && [[self delegate] respondsToSelector:@selector(manager:didRecievePublicTimeline:)]) { 
      [[self delegate] manager:self didRecievePublicTimeline:tweets]; 
     } 
     [tweets release]; 
    } 
} 

나는 단지 공공 타임 라인을 얻기를위한 코드를 게시했다.

+0

트위터 요청을 처리하는 데 사용 된 코드를 표시 할 수 있습니까? –

+0

왜'[self foo]'가 아닌'[self performSelector : @selector (foo)]'를 계속 사용합니까? –

+0

나중에 메서드 선언을 구현했으며 코드를 업데이트하지 않았습니다. 이것이 원인 일 수 있다고 생각하니? –

답변

3

아마도 비 동시성 NSOperation을 사용하고있을 것입니다. 동시성 프로그래밍 안내서의 Operation Queues을 참조하십시오. 특히 "동시 대 비 동시 작업"절을 참조하십시오.

일반적으로 이러한 종류의 기능을 수행하려면 일반적으로 NSOperation이 아닌 비동기식 NSURLConnection을 사용하는 것이 좋습니다. 이것은 정확히 NSURLConnection이 잘하는 것입니다.

관련 문제