2

NSOperationQueue と NSOperation を使用して、Twitter への要求を実行しています。ただし、このリクエストを実行して結果を受け取ると、すべての結果がダウンロードされるまでアプリケーションが数秒間フリーズします。

これを止める方法はありますか?

編集 - -

私のコードは次のとおりです。

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

公開タイムラインを取得するためのコードのみを投稿しました。

4

1 に答える 1

3

おそらく非並行を使用していますNSOperation同時実行プログラミング ガイドのオペレーション キュー、特に「同時操作と非同時操作」のセクションを参照してください。

ただし、通常、この種の機能を実行するにNSURLConnectionは、独自のものではなく非同期を使用することを強くお勧めします。NSOperationこれはまさにNSURLConnection非常にうまくいくものです。

于 2011-05-01T16:45:31.460 に答える