Answer the question
In order to leave comments, you need to log in
Objective c, how to wait until block finishes?
There is a function that should return an array either from a file or download from the server and put it in this file. I ask this function to return an array, it returns an empty one, and after that it executes a request to the server, how to return data after downloading from the server?
Roughly what I have:
-(NSArray*)getData{
__block NSArray *returnData = nil;
if([self internetConnect]){
[self reloadData:^(NSArray* loadData){
returnData = loadData;
}];
}else{
//тут проблем нет
}
return returnData;
}
Answer the question
In order to leave comments, you need to log in
You want the current thread to wait for the asynchronous request to complete.
-(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;
}
+(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;
}
-(void)testFunc {
dispatch_async([MyClass sharedConcurrentQueue], ^(void){
// Все, что хотим запустить и идти дальше, не дожидаясь
});
dispatch_sync([MyClass sharedConcurrentQueue], ^(void){
// Все, что хотим запустить и подождать завершения
});
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question