如何在函数中立即执行块? [英] How to execute a block immediately in a function?

查看:89
本文介绍了如何在函数中立即执行块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上搜索,但是发现的却不是我期望的.

I search on the web, but what I found is not what I expect.

我有一个内部带有块的函数,该函数在对块进行处理之前会返回.所以我的函数返回nil ...

I have a function with a block inside, and this function return before doing the treatment into the block. So my function return nil...

NSString* returnTheFolder()
{
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    GTLServiceDrive *service;
    __block NSMutableDictionary* titlesAndIdentifiers;
    __block __strong NSString* rootFolder;
    __block NSArray* allIdentifiers;
    NSString* userDefaultValue = [userDefaults objectForKey:@"User1"];
    titlesAndIdentifiers = [[NSMutableDictionary alloc]init];
    service = [[GTLServiceDrive alloc] init];
    service.authorizer = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:KeychainForName clientID:ClientID clientSecret:ClientSecret];
GTLQueryDrive *query =
[GTLQueryDrive queryForFilesList];
query.maxResults = 9999999;
query.q = @"'root' in parents";  
    [service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, GTLDriveFileList* files, NSError *error) {
        for (GTLDriveFile *folder in files)
        {
            if ([folder.mimeType isEqualToString:@"application/vnd.google-apps.folder"])
            {
                [titlesAndIdentifiers setValue:folder.identifier forKey:folder.title];
                    allIdentifiers = [[NSArray alloc]initWithArray:[titlesAndIdentifiers allKeysForObject:userDefaultValue]];
                    rootFolder = [allIdentifiers objectAtIndex:0];
            }
        }
    }];
    return rootFolder;
}

我可以使用哪种方法立即在函数中执行块?

Which method can I use for execute a block immediately in my function ?

非常感谢大家!!!

推荐答案

您正在使用的Google库被设计为异步的-它正在对Web服务进行调用,并且它们可能需要花费很长时间才能完成.在考虑同步解决方案时,必须考虑到这一点-您可能会长时间阻塞.

The Google library you are using is designed to be asynchronous - it is making calls to a web service and they can take an arbitrarily long time to complete. You must take this into consideration when thinking of a synchronous solution - you may block for an arbitrarily long time.

最佳解决方案是重新设计代码,使其异步.

The best solution for you is to redesign your code so it to is asynchronous.

如果由于某种原因无法使代码异步,则可以使异步调用看起来是同步的,但是必须小心.本质上,您所需要做的就是使用一个信号量:让异步调用的回调块向该信号量发出信号,并在使异步调用等待该信号量之后. 但是要使此功能正常工作,您需要知道您正在等待信号量的线程与用于回调的线程不同-否则将阻止回调.您需要确定Google库对保证将在其上调用回调的线程的保证,并适当地编写您的代码.

If you cannot make your code asynchronous for some reason then you can make an asynchronous call appear synchronous, but you must be careful. In essence all you need to do is use a semaphore: have the callback block to the asynchronous call signal the semaphore, and after making the asynchronous call wait on the semaphore. However for this to work you need to know that the thread you are waiting on the semaphore is not the same thread that will be used for the callback - or the callback will be blocked. You need to determine what guarantees the Google library makes about the thread the callback will be invoked on and write your code appropriately.

如果听起来很复杂,请回到第一个建议-使您的代码异步!

And if all that sounds too complicated go back to the first recommendation - make your code asynchronous!

HTH

这篇关于如何在函数中立即执行块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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