iphone - AFNetworking冻结其他操作

  显示原文与译文双语对照的内容
0 0

我正在构建一个聊天应用程序,它反复调用一个使用 AFNetworking的web服务。 聊天屏幕不断地轮询这里服务以获得新的聊天消息。 服务相关的一切都正常运行,但是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];
}

我每次重新加载 table 视图,但UI仍然冻结。 我尝试使用 background 线程,但这一点也不奏效。

时间:原作者:6个回答

0 0

我知道这是个旧问题,但我刚刚 bumped 。 只需通知AFNetworking使用调度的异步队列执行连接操作,并返回在主队列中检索到的NSData的格式。 所以AFNetworking绝对不是问题。

我的建议是尝试执行 ParseJson: 和 GetAllIncomingMessages: 在分隔的线程或者者分派异步队列时,你会看到你的UI不再冻结。

就像这样:

static dispatch_queue_t your_app_queue() {
 static dispatch_once_t onceToken;
 static dispatch_queue_t _myQueue;
 dispatch_once(&onceToken, ^{
 _myQueue = dispatch_queue_create("com.myapp.queue", DISPATCH_QUEUE_SERIAL);
 });
 return _myQueue;
}
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest: request
 success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
 __block myClass = self;
 dispatch_async(your_app_queue(), ^{
 [myClass ParseJson:(NSDictionary *)JSON];
 [myClass GetAllIncomingMessages];
 });
 }
 failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
 __block myClass = self;
 dispatch_async(your_app_queue(), ^{
 [myClass GetAllIncomingMessages];
 dispatch_async(dispatch_get_main_queue(), ^{
 UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error"
 message:[NSString stringWithFormat:@"%@",error]
 delegate:nil
 cancelButtonTitle:@"OK" otherButtonTitles:nil];
 [av show];
 });
 });
 }];

或者:

AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest: nil
 success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
 [self performSelectorInBackground:@selector(ParseJson:) withObject:JSON];
 [self performSelectorInBackground:@selector(GetAllIncomingMessages) withObject:nil];
 }
 failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
 [self performSelectorInBackground:@selector(GetAllIncomingMessages) withObject:nil];
 UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error"
 message:[NSString stringWithFormat:@"%@",error]
 delegate:nil
 cancelButtonTitle:@"OK" otherButtonTitles:nil];
 [av show];
 }];

而且应该很好希望这个帮助 !

原作者:
...