gpt4 book ai didi

ios - 强制一种方法等待另一种方法完成

转载 作者:行者123 更新时间:2023-12-01 17:52:17 24 4
gpt4 key购买 nike

在我转到目标 View Controller 之前的原始 View Controller 中,我调用了一个获取我的关键参数的方法,然后我在以下方法中在我的目标 View Controller 中设置了关键参数。但是,关键参数设置在 doSomethingToGetKey 之前。方法已完成,因此传递了一个空值。我想知道是否有办法让第二种方法等待第一种方法完成。

原始 View Controller :

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

[self doSomethingToGetKey];
[segue.destinationViewController setKey:_key];
}

-(void)doSomethingToGetKey:(ACAccount *)account{

[_apiObject postReverseOAuthTokenRequest:^(NSString *authenticationHeader) {

APIObject *api = [APIObject APIOSWithAccount:account];
[api verifyCredentialsWithSuccessBlock:^(NSString *username) {


[api postReverseAuthAccessTokenWithAuthenticationHeader:authenticationHeader successBlock:^(NSString *oAuthToken, NSString *oAuthTokenSecret, NSString *userID, NSString *screenName) {

_key = oAuthToken;
_secret = oAuthTokenSecret;
_userId = userID;



} errorBlock:^(NSError *error) {

NSLog(@"ERROR 1, %@", [error localizedDescription]);
exit(1);

}];

} errorBlock:^(NSError *error) {

NSLog(@"ERROR 2: %@",error.description);
exit(1);

}];

} errorBlock:^(NSError *error) {

NSLog(@"ERROR 3: %@",error.description);
exit(1);

}];

};

最佳答案

通常,该模式涉及使用完成 block ,例如:

- (void) doSomethingToGetKeyWithCompletionHandler:(void (^)(NSString *key))completion
{
// perform asynchronous request

// in its completion handler, call the `completion` block parameter given above

[api doingSomeOtherAsynchronousMethodWithCompletion:^{
completion(key);
}];
}

然后,而不是:
[self doSomethingToGetKey];
[self doSomethingElseWithKey:_key];

你可以这样做:
[self doSomethingToGetKeyWithCompletionHandler:^(NSString *key){
[self doSomethingElseWithKey:key];
}];

但是,在这种情况下,您尝试在 prepareForSegue 内完成所有这些操作。 .这完全改变了问题,因为在调用此异步方法之前仍将执行 segue,从而违背了此完成 block 模式的目的。

因此,您还想更改您的按钮(或其他)以不执行 segue 本身,而是调用 IBAction ,然后让它以编程方式执行 segue ( as shown here )。因此,您最终会得到 IBAction像:
- (IBAction)tappedButton:(id)sender 
{
// perhaps update UI so user knows something slow is happening first,
// e.g., show a `UIActivityIndicatorView`

[self doSomethingToGetKeyWithCompletionHandler:^(NSString *key){

// remove that `UIActivityIndicatorView`, if you showed one

[self performSegueWithIdentifier:@"Details" sender:self];
}];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Details"]) {
[segue.destinationViewController setKey:_key]
}
}

或者,您可以删除所有这些,只需执行 segue,然后让目标 View Controller 出现,显示事件指示器 View ,执行 api 调用,然后删除事件指示器 View 。这是对代码的更戏剧性的重构,但可能是更好的用户体验。

但是,底线,来自 prepareForSegue即使您使用完成 block 模式,您也不能执行异步操作,并期望目标 View Controller 等待它。您必须使用 IBAction如果您想在执行 segue 之前完成所有这些操作,请接近。或者,就像我在这里建议的那样,只需在目标 View Controller 中执行所有这些异步请求内容。出于某种原因,立即转换到目标 View Controller (并在那里显示 UIActivityIndicatorView)比延迟目标 View Controller 的呈现更令人满意(即使它们在功能上非常相似)。

关于ios - 强制一种方法等待另一种方法完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25967192/

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