获取数据后移至 rootViewController
Move to rootViewController once data has been fetched
我是 iOS - Objective C 的新手,我正在尝试实现登录功能。我的情节提要图如下所示:
因此,当有人单击帐户选项时,它会进入配置文件视图控制器,并在 viewWillAppear
方法中检查是否有任何用户数据。如果不是,它会执行一个 segue 到登录视图控制器。
当用户单击登录按钮并且 api 调用成功时,我想返回到配置文件视图控制器。问题是,虽然我确实设法取回了数据,但它不会让我返回到 Profile 视图控制器。
// code that creates the request and adds POST data and headers is not added to keep code sample to minimum
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
// execute data task
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *responseData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if (responseData != nil) {
if ([[responseData valueForKey:@"status"] isEqualToString:@"OK"]) {
//delegate to pass the user data to Profile view controller
[self.delegate passUserDetails:[responseData objectForKey:@"user"]];
[self.navigationController popToRootViewControllerAnimated:YES];
} else if ([[responseData valueForKey:@"status"] isEqualToString:@"ERROR"]) {
NSLog(@"%@", [responseData valueForKey:@"error"]);
}
} else {
NSLog(@"Something went wrong, please try again");
}
}];
[dataTask resume];
我在控制台上遇到的问题是
This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.
我在以前的帖子中读到我可以使用 dispatch_async
作为问题的可能解决方案,但我不明白具体在哪里。如果那是正确的,我需要将它用作我的 completionHandler
的包装器吗?我做错了什么(可能)吗?有没有其他方法可以实现我的想法?
NSURLSession dataTaskWithRequest...
调用的 completionHandler 在后台线程上 运行。大多数 UIKit 调用需要从主线程进行。
将这些调用包含在:
dispatch_async(dispatch_get_main_queue(), ^{
// insert your code here
});
在你的 completionHandler 中。
你的情况:
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *responseData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if (responseData != nil) {
if ([[responseData valueForKey:@"status"] isEqualToString:@"OK"]) {
//delegate to pass the user data to Profile view controller
[self.delegate passUserDetails:[responseData objectForKey:@"user"]];
dispatch_async(dispatch_get_main_queue(), ^{
[self.navigationController popToRootViewControllerAnimated:YES];
};
} else if ([[responseData valueForKey:@"status"] isEqualToString:@"ERROR"]) {
NSLog(@"%@", [responseData valueForKey:@"error"]);
}
} else {
NSLog(@"Something went wrong, please try again");
}
}];
如果 passUserDetails
进行任何更新 UI 的调用,您也需要将其包括在内。
我认为你应该简单地以模态方式呈现登录视图控制器,在登录成功后你可以关闭登录视图控制器.....由于模态呈现用于从用户那里获取短数据。所以以模态方式呈现登录控制器将最好的练习。
如果你想返回到根视图控制器,那么你可以使用下面的代码...我正在从我的 phone 回答所以输入句子 Ho avoid naming conventions error。
[self.navigationControllerpopTorootviewController];
我是 iOS - Objective C 的新手,我正在尝试实现登录功能。我的情节提要图如下所示:
因此,当有人单击帐户选项时,它会进入配置文件视图控制器,并在 viewWillAppear
方法中检查是否有任何用户数据。如果不是,它会执行一个 segue 到登录视图控制器。
当用户单击登录按钮并且 api 调用成功时,我想返回到配置文件视图控制器。问题是,虽然我确实设法取回了数据,但它不会让我返回到 Profile 视图控制器。
// code that creates the request and adds POST data and headers is not added to keep code sample to minimum
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
// execute data task
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *responseData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if (responseData != nil) {
if ([[responseData valueForKey:@"status"] isEqualToString:@"OK"]) {
//delegate to pass the user data to Profile view controller
[self.delegate passUserDetails:[responseData objectForKey:@"user"]];
[self.navigationController popToRootViewControllerAnimated:YES];
} else if ([[responseData valueForKey:@"status"] isEqualToString:@"ERROR"]) {
NSLog(@"%@", [responseData valueForKey:@"error"]);
}
} else {
NSLog(@"Something went wrong, please try again");
}
}];
[dataTask resume];
我在控制台上遇到的问题是
This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.
我在以前的帖子中读到我可以使用 dispatch_async
作为问题的可能解决方案,但我不明白具体在哪里。如果那是正确的,我需要将它用作我的 completionHandler
的包装器吗?我做错了什么(可能)吗?有没有其他方法可以实现我的想法?
NSURLSession dataTaskWithRequest...
调用的 completionHandler 在后台线程上 运行。大多数 UIKit 调用需要从主线程进行。
将这些调用包含在:
dispatch_async(dispatch_get_main_queue(), ^{
// insert your code here
});
在你的 completionHandler 中。
你的情况:
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *responseData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if (responseData != nil) {
if ([[responseData valueForKey:@"status"] isEqualToString:@"OK"]) {
//delegate to pass the user data to Profile view controller
[self.delegate passUserDetails:[responseData objectForKey:@"user"]];
dispatch_async(dispatch_get_main_queue(), ^{
[self.navigationController popToRootViewControllerAnimated:YES];
};
} else if ([[responseData valueForKey:@"status"] isEqualToString:@"ERROR"]) {
NSLog(@"%@", [responseData valueForKey:@"error"]);
}
} else {
NSLog(@"Something went wrong, please try again");
}
}];
如果 passUserDetails
进行任何更新 UI 的调用,您也需要将其包括在内。
我认为你应该简单地以模态方式呈现登录视图控制器,在登录成功后你可以关闭登录视图控制器.....由于模态呈现用于从用户那里获取短数据。所以以模态方式呈现登录控制器将最好的练习。
如果你想返回到根视图控制器,那么你可以使用下面的代码...我正在从我的 phone 回答所以输入句子 Ho avoid naming conventions error。 [self.navigationControllerpopTorootviewController];