gpt4 book ai didi

iphone - AFNetworking 卡住其他 Action

转载 作者:行者123 更新时间:2023-11-29 10:56:58 24 4
gpt4 key购买 nike

我正在构建一个使用 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];
}

我每次都重新加载表格 View ,但 UI 仍然卡住。我尝试使用后台线程,但也没有用。

最佳答案

我知道这是一个老问题,但我刚刚碰到它。仅供引用 AFNetworking 使用分派(dispatch)的异步队列来执行连接操作,并返回主队列中检索到的 NSData 的 JSON 格式(您可能已经知道)。所以 AFNetworking 绝对不是问题所在。

我的建议是尝试在单独的线程中执行 ParseJson: 和 GetAllIncomingMessages: 或自己分派(dispatch)异步队列,您会发现您的 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];
}];

应该没问题。希望对您有所帮助!

关于iphone - AFNetworking 卡住其他 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17829420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com