I’m posting this because i’m curious about something.
For example, let’s take a look at this sample code (just as an example):
-(void)doSomething{
for (int i=0;i<10000000000;i++){
nslog(@"%i", int);
}
nslog(@"i'm done!");
}
Is there any way to execute the for loop in the same method (doSomething) but in another thread without the need of doing something like NSOperationQueue(@selector)?
And the NSLog must be executed only after the for loop is finished.
If there’s something out there, example code would be appreciated!
Thank you.
Yes, you can use GCD with
dispatch_async. But thei'm doneNSLog won’t be executed after all the iterations are done. It doesn’t make sense because it would have to wait on the primary (main) thread for the background (iterations) thread to finish.You’d have to execute the whole method in background thread if you don’t want to block the main one for the UI.