1

我正在构建一个聊天应用程序,它使用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 仍然冻结。我尝试使用后台线程,但也没有用。

4

1 回答 1

2

我知道这是一个老问题,但我只是碰到了它。仅供参考,AFNetworking 使用分派的异步队列来执行连接操作,并将主队列中检索到的 NSData 的 JSON 格式(您可能已经知道)返回给您。所以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];
                                                }];

而且应该没问题。希望这有帮助!

于 2014-01-29T13:34:58.553 回答