gpt4 book ai didi

ios - UIKeyboardTaskQueue 线程问题

转载 作者:行者123 更新时间:2023-11-29 10:41:33 26 4
gpt4 key购买 nike

我是 iOS 开发的新手,我已经被这个错误困扰了一段时间。我正在制作一个使用网络服务的简单应用程序。现在我有两个 View Controller 。登录 View Controller (及其 NIB 文件)和主视图 Controller (及其 NIB 文件)。当我创建应用程序时,我选择了一个空应用程序,所以我没有 Storyboard。相反,我正在使用 UINavigationController。当我运行代码时,输​​入用户名和密码并在登录 View 中按提交后出现以下错误:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[UIKeyboardTaskQueue waitUntilAllTask​​sAreFinished] 只能从主线程调用。”

这是我的提交按钮代码:

-(IBAction)logIn:(id)sender{
UIApplication *application = [UIApplication sharedApplication];
application.networkActivityIndicatorVisible = YES;

[_loginNetworkingContorller checkCredentialsWithUsername:self.username.text withPassword:self.password.text completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(!error){
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
if (httpResp.statusCode == 200) {
NSLog(@"SUCESS");
NSDictionary *credentials = @{self.username.text: self.password.text};
[KeychainUserPass save:@"MY APP" data:credentials];
UIViewController *mainView = [[RDMainViewController alloc] initWithNibName:@"RDMainViewController" bundle:nil];

[self.navigationController pushViewController:mainView animated:YES];
}
else{
NSLog(@"ERROR");
}
}
else{
NSLog(@"ERROR");
}
}];
}

下面是函数的代码

[_loginNetworkingContorller checkCredentialsWithUsername:self.username.text withPassword:self.password.text completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)

-(void)checkCredentialsWithUsername:(NSString *)username withPassword:(NSString *)password completionHandler:(void (^)(NSData *data,NSURLResponse *response, NSError *error))myCompletion
{
NSString *requestString = @"SOME WEBSITE";
NSURL *url = [NSURL URLWithString:requestString];
NSURLRequest *req = [NSURLRequest requestWithURL:url];

NSData *userPasswordData = [[NSString stringWithFormat:@"%@:%@", username, password] dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64EncodedCredential = [userPasswordData base64EncodedStringWithOptions:0];
NSString *authString = [NSString stringWithFormat:@"Basic %@", base64EncodedCredential];

NSURLSessionConfiguration *sessionConfig=[NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.HTTPAdditionalHeaders=@{@"Authorization":authString};

self.session=[NSURLSession sessionWithConfiguration:sessionConfig];

NSURLSessionDataTask *dataTask = [self.session dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
myCompletion(data, response, error);
}];

[dataTask resume];
}

我现在卡住了,我不太确定问题出在哪里,尤其是因为我没有对键盘进行任何操作。我感觉我的积木有问题,但我不确定是什么问题。任何帮助将不胜感激。

最佳答案

嘿,在我收到网络电话的回复后,我遇到了同样的问题。我能够通过执行以下操作解决它:

 //do something with response

dispatch_async(dispatch_get_main_queue()) { () -> Void in

// continue with program by calling next step on main thread
}

我认为如果你推送到下一个 View Controller iOS 尝试在非主线程上执行它,会导致错误,但我不是 100% 是准确的

关于ios - UIKeyboardTaskQueue 线程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24320796/

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