Objective c how to wait for the end of the block?

There is a function which should return an array or from a file, or download it from the server and put in this file. I ask this function to return an array, it returns empty, and then executes the query on the server, how to return data after downloading from the server?
Roughly here is what I have:
-(NSArray*)getData{
 __block NSArray *returnData = nil;
 if([self internetConnect]){
 [self reloadData:^(NSArray* loadData){
 returnData = loadData;
}];
}else{
 //no problems here
}
 return returnData;
}

In a method reload data as there is a block that loads the data in the common toil like this block with a block in the compartment which loads the data and put into array to return from this method, getData, after all progresivos.
Thanks in advance for your help.
September 26th 19 at 07:23
1 answer
September 26th 19 at 07:25
Solution
You need to make the current thread wait for the completion of the asynchronous request.

-(NSArray*)getData{
 __block NSArray *returnData = nil;
 if([self internetConnect]){
 dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void){
 [self reloadData:^(NSArray* loadData){
 returnData = loadData;
}];
});
}else{

}
 return returnData;
}

You can also start your own competitive place

+(dispatch_queue_t)sharedConcurrentQueue {
 static dispatch_queue_t queue = nil;
 static dispatch_once_t onceToken = 0;

 dispatch_once(&onceToken, ^{
 queue = dispatch_queue_create("MyQueueName", DISPATCH_QUEUE_CONCURRENT);
});

 return queue;
}

And put any problems thus

-(void)testFunc {
 dispatch_async([MyClass sharedConcurrentQueue], ^(void){
 // Whatever you want to start and go further, without waiting
});

 dispatch_sync([MyClass sharedConcurrentQueue], ^(void){
 // Whatever you want to run and wait for completion
});
}

You can read about this here.
Or Google "Grand Central Dispatch".
Thanks for the reply, you said all right, did so, there was another problem that the loader made a class and also in asynchronous mode return data through the unit, in General to this block did not reach the expectation, made a separate simultaneous request, and put it all in place, still works) - lucienne.Miller commented on September 26th 19 at 07:28

Find more questions by tags Objective-C