1
我建立一個聊天應用程序,它使用AFNetworking
重複調用的Web服務。聊天屏幕不斷地輪詢此服務以獲得新的聊天消息。與服務相關的一切工作正常,但UI保持凍結,並且沒有任何按鈕正在工作。AFNetworking凍結等措施
下面是代碼:
- (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];
}
我重裝每次表視圖,而且用戶界面仍然凍結。我嘗試使用後臺線程,並沒有工作。
試圖調用在後臺線程此方法。 –
注意它是'AFNetworking'不'AFINetworking' –
你的代碼看起來很好。它應該有異步工作。如果反覆調用這個Web服務的話,我覺得你的代碼搞一次又一次地調用這個Web服務,其結果遞歸運行(您的代碼可以遞歸某處運行方法的機會呀你處理大量的主數據線)。遞歸代碼凍結您的應用程序。檢查你的類和方法調用層次結構。 – Tirth