gpt4 book ai didi

ios - 等到委托(delegate)方法在 ios 中完成执行

转载 作者:行者123 更新时间:2023-11-28 18:22:10 27 4
gpt4 key购买 nike

   -(void)method1
{
[self method2];
[self method3]; //After finishing execution of method2 and its delegates I want to execute method3
}

此处,方法 2 在调用时开始运行,但在执行其委托(delegate)方法之前,方法 3 开始执行。如何避免这种情况?任何建议或代码请

我在方法 2

中调用了一个与其委托(delegate)的 nsurl 连接
 -(void)method2
{
....
connection= [[NSURLConnection alloc] initWithRequest:req delegate:self ];
....
}


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

}

-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data
{

}
..
..

最佳答案

使用 block - 会更容易处理:

[NSURLConnection sendAsynchronousRequest:request
queue:[[NSOperationQueue alloc] init]

completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error)
{

if ([data length] >0 && error == nil) {
// parse your data here

[self method3];

dispatch_async(dispatch_get_main_queue(), ^{

// call method on main thread, which can be used to update UI stuffs
[self updateUIOnMainThread];
});
}
else if (error != nil) {
// show an error
}
}];

关于ios - 等到委托(delegate)方法在 ios 中完成执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18459161/

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