为什么dispatch_async之后的块(dispatch_get_main_queue()永远不会被调用?

时间:2016-01-28 15:58:08

标签: objective-c xcode macos grand-central-dispatch dispatch-async

我想异步执行一些代码,因此开始使用GCD for OSX / iOS 目前我正在使用函数 dispatch_async() 当我想在另一个线程上同时执行某些操作时,我使用函数 dispatch_get_global_queue()
当我想将结果发送到主线程时,我使用函数 dispatch_get_main_queue()
但结果永远不会到达主线。

在调试器中设置断点(在 dispatch_async 行)时,函数 dispatch_async之后的块(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0) 有时只执行,但调试器通常会忽略它 当块执行并且执行流程到达 dispatch_async(dispatch_get_main_queue()的断点时,之后的块总是被忽略。

如果没有 dispatch_async(dispatch_get_global_queue) dispatch_async(dispatch_get_main_queue),代码将按原样执行,尽管是同步的。

我的问题是为什么 dispatch_async(dispatch_get_global_queue()块中的 dispatch_async(dispatch_get_main_queue()永远不会被执行?
同样,为什么每次都不会执行 dispatch_async(dispatch_get_global_queue()块?

我的发展环境是 操作系统: OS X 10.11.3
IDE: Xcode 7.2
编译器: Apple LLVM版本7.0.2(clang-700.1.81)
目标: x86_64-apple-darwin15.3.0

这是一个简单的例子,它可以重现不稳定的行为(OS X控制台应用程序):

TestClass.h

#ifndef TestClass_h
#define TestClass_h

@interface TestClass : NSObject {
}

- (void)testMethod:(NSString *)testString withCompletionBlock:(void(^)(NSString *blockResult, NSError __autoreleasing *error))completionBlock;

@end

#endif

TestClass.m

#import <Foundation/Foundation.h>
#import "TestClass.h"

@implementation TestClass

- (void)testMethod:(NSString *)testString withCompletionBlock:(void(^)(NSString *blockResult, NSError __autoreleasing *error))completionBlock {
    __block NSString *stringResult = nil;

    if (completionBlock) {
        __block NSError *resultError = nil;


       dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            // THIS BLOCK IS CALLED ONLY SOMETIMES, MOST OF THE TIME IT IS IGNORED.

            if ([testString isEqual: @"Error string"]) {
                NSDictionary *errorUserInfo = @{ NSLocalizedDescriptionKey: @"This is an error.", NSLocalizedFailureReasonErrorKey: @"", NSLocalizedRecoverySuggestionErrorKey: @"" };                
                resultError = [[NSError alloc] initWithDomain:@"com.test.TestErrorDomain" code:10 userInfo:errorUserInfo];
            }
            else {
                stringResult = testString;
            }

            dispatch_async(dispatch_get_main_queue(), ^{
                // THIS BLOCK NEVER GETS EXECUTED.

                completionBlock(stringResult, resultError);
            });
        });
    }
}

@end

的main.m

#import <Foundation/Foundation.h>
#import "TestClass.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        __block NSString *resultString = nil;
        TestClass * testObject = [[TestClass alloc] init];

        // Output for this call should be: The result string is: Test string.
        [testObject testMethod:@"Test string" withCompletionBlock:^(NSString *blockString, NSError __autoreleasing *error) {
            resultString = blockString;

            if (resultString) {
                NSLog(@"The result string is: %@.", resultString);
            }
        }];

        // Output for this call should be: Error: This is an error.
        [testObject testMethod:@"Error string" withCompletionBlock:^(NSString *blockString, NSError __autoreleasing *error) {
            resultString = blockString;

            if (!resultString) {
                if (error) {
                    NSLog(@"Error: %@", [error localizedDescription]);
                }
                else {
                    NSLog(@"Error not recognized!");
                }
            }
        }];
    }

    return 0;
}

2 个答案:

答案 0 :(得分:4)

这是因为main()函数退出后会立即释放TestClass对象。主要是异步,testMethod:不会阻塞main()中的指针。

尝试在main()的末尾添加一个信号量。应在testMethod #/:param块中发信号通知此信号量。

答案 1 :(得分:2)

这是带有信号量的工作示例,如@ CharlesThierry的回答中所述。 我还提供了一些有助于说明执行流程的NSLog:

TestClass.h

#ifndef TestClass_h
#define TestClass_h

@interface TestClass : NSObject {
}

- (void)testMethod:(NSString *)testString withSemaphore:(dispatch_semaphore_t)sem withCompletionBlock:(void(^)(NSString *blockResult, NSError __autoreleasing *error))completionBlock;

@end

#endif

TestClass.m

#import <Foundation/Foundation.h>
#import "TestClass.h"

@implementation TestClass

- (void)testMethod:(NSString *)testString withSemaphore:(dispatch_semaphore_t)sem withCompletionBlock:(void(^)(NSString *blockResult, NSError __autoreleasing *error))completionBlock {
    __block NSString *stringResult = nil;

    if (completionBlock) {
        __block NSError *resultError = nil;

        NSLog(@"INSIDE TEST METHOD, OUTSIDE DISPATCH ASYNC");

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSLog(@"INSIDE THE DISPATCH ASYNC TO THE GLOBAL QUEUE");

            if ([testString isEqual: @"Error string"]) {
                NSDictionary *errorUserInfo = @{ NSLocalizedDescriptionKey: @"This is an error.", NSLocalizedFailureReasonErrorKey: @"", NSLocalizedRecoverySuggestionErrorKey: @"" };
                resultError = [[NSError alloc] initWithDomain:@"com.test.TestErrorDomain" code:10 userInfo:errorUserInfo];
            }
            else {
                stringResult = testString;
            }

            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"INSIDE THE DISPATCH ASYNC TO THE MAIN QUEUE!");
                completionBlock(stringResult, resultError);
                dispatch_semaphore_signal(sem);
            });
        });
    }
}

@end

的main.m

#import <Foundation/Foundation.h>
#import "TestClass.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"BEFORE BLOCK");

        __block NSString *resultString = nil;
        TestClass * testObject = [[TestClass alloc] init];
        dispatch_semaphore_t sem = dispatch_semaphore_create(0);

        [testObject testMethod:@"Test string" withSemaphore:sem withCompletionBlock:^(NSString *blockString, NSError __autoreleasing *error)
         {
             NSLog(@"INSIDE FIRST BLOCK");

             resultString = blockString;

             if (resultString)
             {
                 NSLog(@"The result string is: %@.", resultString);
             }
         }];

        [testObject testMethod:@"Error string" withSemaphore:sem withCompletionBlock:^(NSString *blockString, NSError __autoreleasing *error)
         {
             NSLog(@"INSIDE SECOND BLOCK");

             resultString = blockString;

             if (!resultString)
             {
                 if (error)
                 {
                     NSLog(@"Error: %@", [error localizedDescription]);
                 }
                 else
                 {
                     NSLog(@"Error not recognized!");
                 }
             }
         }];

        NSLog(@"AFTER BLOCK!");

        while (dispatch_semaphore_wait(sem, DISPATCH_TIME_NOW))
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
    }

    return 0;
}