gpt4 book ai didi

ios - 等待 block 完成吗?

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

是否可以等待某些服务器查询块结束其操作?我的无效代码如下;

 + (BOOL) loginQueryFromServer:(NSDictionary *)parameters{

__block BOOL loginQueryResult = NO;

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

AFHTTPRequestOperation *operation = [manager POST:@"http://domainname/login/" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

NSLog(@"Response1: %@",responseObject);
loginQueryResult = YES;
NSLog(@"Response2: %@", loginQueryResult ? @"YES" : @"NO");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"Error: %@", error);
loginQueryResult = NO;
}];
[operation start];
[operation waitUntilFinished];

NSLog(@"Response3: %@", loginQueryResult ? @"YES" : @"NO");
return loginQueryResult;
}

结果:(如您所见; Response3首先记录)
2014-04-09 21:11:17.072 aaa[1010:70b] Response3: NO
2014-04-09 21:11:17.073 aaa[1010:70b] Response1: {
reply = "Login successful!";
}
2014-04-09 21:11:17.081 aaa[1010:70b] Response2: YES

最佳答案

我知道这并不能完全回答问题-但我建议您重写loginQueryFromServer:方法以接受完成块本身,以便它是异步的(我认为这最终是您想要做的事情):

+ (void) loginQueryFromServer:(NSDictionary *)parameters completion:(void(^)(BOOL response, NSError *error))completionBlock {

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

AFHTTPRequestOperation *operation = [manager POST:@"http://domainname/login/" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

completionBlock(YES, nil);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

completionBlock(NO, error);
}];
[operation start];
}

像这样使用它:
[YourClass loginQueryFromServer:someDictionary completion:^(BOOL response, NSError *error) {

// do something with the response and the error if it exists
}];

关于ios - 等待 block 完成吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22970956/

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