Since the block shares the area with the parent, the easiest way is not to specify the return type at all. For instance:
- (NSData *)returnData:(NSString *)urlString{ dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); __block NSData *data;
If you want to specify a return type, you cannot use dispatch_sync()
, because it only accepts blocks without a return type. Example of using blocks manually:
typedef NSData *(^MyDataBlock)(); MyDataBlock getData = ^ NSData *() { return [NSData data]; }; NSData *data = getData();
Please note that since in the second example you are not using dispatch_sync()
, this code will be executed immediately in the current queue, and not wait until the processor time is available. Waiting for a processor to have some downtime may be faster than executing it immediately.
The advantage of this approach is much more flexible, you can pass the getData
variable to other methods, etc., and they can even execute it inside their own dispatch_sync()
call to take advantage of the GCD priority system.
source share