NS操作与NSOperationQueue 冻结应用程序

发布于 2024-11-04 13:36:44 字数 5914 浏览 2 评论 0原文

我正在使用 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];
}

Twitter Manager:

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

我只发布了获取公共时间线的代码。

I am using NSOperationQueue and NSOperation to perform requests to twitter. However when I am performing this request, and receiving results, the application freezes for a few seconds until all the results have been downloaded.

Is there a way to stop this happening?

Edit----

My Code is:

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

Twitter Manager:

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

I have only posted the code for getting the public timeline.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒带 2024-11-11 13:36:44

您可能正在使用非并发 NSOperation。请参阅操作队列,尤其是“并发与非并发操作”部分。

不过,通常情况下,我强烈建议使用异步 NSURLConnection 而不是您自己的 NSOperation 来执行此类功能。这正是 NSURLConnection 做得很好的地方。

You're probably using a non-concurrent NSOperation. See Operation Queues in the Concurrency Programming Guide, especially the section on "Concurrent Versus Nonconcurrent Operations."

Typically, though, I'd strongly recommend using an asynchronous NSURLConnection rather than your own NSOperation to perform this kind of function. This is exactly what NSURLConnection does very well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文