如何在ios中进行函数调用等待,直到该函数内的块完全执行? [英] How to make a function call in ios to wait, till the block inside that function is executed completely?

查看:266
本文介绍了如何在ios中进行函数调用等待,直到该函数内的块完全执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下功能中,我使用了一个块。但是当我调用这个函数时,它甚至在执行块之前就会返回。我知道Block inturn使用线程并单独执行,以便函数不会等待它返回。但是,有没有其他方法可以使函数执行等待,或任何其他方式来实现此块的功能而不使用块本身?

Inside the following function, I've used a block. But when I call this function, it is returned even before the block is executed. I understood that Block inturn uses the threads and executes separately so that the function doesnt wait for it to return. But, Is there any other way I could make the function execution wait, or any other way to achieve the functionality of this block without using the block itself ?

-(int)findCurrentZip
{
        CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:[self findCurrentLatitude]
                                                              longitude:[self findCurrentLongitude]];
         int zipcode;
        self.myGeocoder = [[CLGeocoder alloc] init];
        [self.myGeocoder 
         reverseGeocodeLocation:userLocation
         completionHandler: (id)^(NSArray *placemarks, NSError *error) {
             if (error == nil && [placemarks count] > 0)
             {
                 NSLog(@"Placemarks: %@",placemarks);
                 CLPlacemark *placemark = [placemarks objectAtIndex:0]; 
                 NSLog(@"Country = %@", placemark.country);
                 NSLog(@"Postal Code = %@", placemark.postalCode);
                 zipcode = (int)placemark.postalCode;
                 NSLog(@"Locality = %@", placemark.locality);
                 NSLog(@"Country%@",[placemarks lastObject]);
             }
             else if (error == nil && [placemarks count] == 0)
             {
                 NSLog(@"No results were returned.");
             }
             else if (error != nil)
             {

             }
        }];

        return zipcode;
    }


推荐答案

首先,我建议重新考虑你的设计。而不是从此方法返回zipCode值,而是调用completionHandler中的其他方法(创建协议/委托或其他)。 reverseGeocodeLocation :: 方法可能需要一些时间,并且您不希望暂停等待结果的主线程的执行。

First, I would suggest to rethink your design. Instead of returning the zipCode value from this method, call into some other method in the completionHandler (create a protocol / delegate or whatever). The reverseGeocodeLocation:: method can take some time, and you don't want to pause execution of the main thread waiting on the result.

如果你想阻止,你可以考虑使用(滥用?)a dispatch_semaphore_t 。在调用 reverseGeocodeLocation :: 之后,将其初始化为0和 dispatch_semaphore_wait 。在completionHandler中用 dispatch_semaphore_signal 发出信号。

If you do want to block though, you might consider using (abusing?) a dispatch_semaphore_t. Initialize it to 0 and dispatch_semaphore_wait after the call to reverseGeocodeLocation::. In the completionHandler signal it with dispatch_semaphore_signal.

更多信息:使用Dispatch信号量来规范使用有限资源

编辑:和其他建议一样,使用__block限定符声明zipCode

edit: and like others suggested, declare zipCode with a __block qualifier

这篇关于如何在ios中进行函数调用等待,直到该函数内的块完全执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆