我正在构建一个聊天应用程序,它使用AFNetworking
. 聊天屏幕不断轮询此服务以获取新的聊天消息。与服务相关的所有内容都运行良好,但 UI 一直冻结,并且所有按钮都不起作用。
这是代码:
- (void)GetAllIncomingMessages
{
NSURL *url = [NSURL URLWithString:weatherUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest: request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[self ParseJson:(NSDictionary *)JSON];
[self GetAllIncomingMessages];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
{
[self GetAllIncomingMessages];
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error "
message:[NSString stringWithFormat:@"%@",error]
delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
}];
[operation setAuthenticationChallengeBlock:
^( NSURLConnection* connection, NSURLAuthenticationChallenge* challenge )
{
if( [[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodHTTPBasic )
{
if( [challenge previousFailureCount] > 0 )
{
// Avoid too many failed authentication attempts which could lock out the user
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
else
{
[[challenge sender] useCredential:[NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession] forAuthenticationChallenge:challenge];
}
}
else
{
// Authenticate in other ways than NTLM if desired or cancel the auth like this:
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}];
[operation start];
}
我每次都重新加载表格视图,但 UI 仍然冻结。我尝试使用后台线程,但也没有用。