0
我有这段代码,但我不明白为什么我必须在主线程上运行此代码。 如果我在后台运行它不会执行发布请求。这是一个错误吗? 我该如何解决这个问题?NSURLConnection当我在后台运行它时出现错误....我该如何解决?
- (void)setRead:(MWFeedItem *)entry
{ [self getToken:YES];
NSString *url=[NSString stringWithFormat:@"https://www.google.com/reader/api/0/edit-tag?a=user/-/state/com.google/read&i=%@&T=%@", entry.identifier, token];
[self postRequestWithURLState:url];
}
- (void)postRequestWithURLState:(NSString *)url
{
NSString *bodyRequest = nil;
NSURL *requestURL = [NSURL URLWithString:url];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];
//NSLog(@"-------------- bodyRequest: %@", bodyRequest);
[theRequest setURL:requestURL];
[theRequest setTimeoutInterval:0.5];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[bodyRequest dataUsingEncoding:NSASCIIStringEncoding]];
[self.oauthAuthentication authorizeRequest:theRequest];
[NSURLConnection connectionWithRequest:theRequest delegate:self];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
这是我的电话:
-(void)segnaLettura:(MWFeedItem *)item{
[reader setRead:item];
}
- (void) segnaread:(MWFeedItem *)item{
[self performSelectorOnMainThread:@selector(segnaLettura:) withObject:item waitUntilDone:NO];
}
我使用segnaread将我的项目标记为在Google阅读器中阅读。当连接速度很慢时,它会冻结我的GUI几秒钟!这就是为什么我需要在后台真正做到这一点。我认为我在主线程上运行的代码行冻结了我的应用程序 – 2012-02-26 00:37:45
如果连接到网络会冻结您的应用程序,那么您的操作不正确。解决方案是不移动到后台线程,但找出您正在同步连接到网络的位置。仪器可以在这里有所帮助。在Time Profiler的主线程中使用“对所有线程状态进行采样”。如果你不确定,这会告诉你你挂在哪里。 – 2012-02-27 14:29:13
非常感谢 – 2012-02-29 23:28:32