0
votes

My iOS app uses a C function for some computing. I want to call it from -(void)viewDidLoad but I don't want it to freeze the main thread. I tried calling it with dispatch_async(dispatch_get_main_queue... block but it still was being called on the main thread. Also including function call into objective-c method and calling [self performSelector: InBackground] didn't work either. Please help. Is there any way I can perform C function in background in objective-c app ?

2
dispatch_get_main_queue() is the problem. You have to use a different queue. - Sulthan
You should read the threading guide by Apple. It doesn't make sense to put something on the main thread if you want it run in background. Also, performSelectorInBackground: will work (obviously you need to encapsulate your C code in an Obj-C function to use this). - Eiko
Yes, putting it on background queue worked. But performSelectorInBackground with the function encapsuled in objc method did not. - Bartek Uchański

2 Answers

2
votes

You have to use a different queue, e.g.:

dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(backgroundQueue, ^{
   // do something
});

See Dispatch Queues for more details.

0
votes

Or you can put it into an obj-c method and call that using:

[NSThread detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument];