Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create two threads in Objective-C

I am very new to Xcode and Objective-C, so I don't really know what I am doing yet! ;-)
Basically I want to have an IPhone Application that can run possibly two threads, which will later use UDP Sockets to communicate with other Apps.

I have put functions to react when the start-Thread/stop-Thread Button in the UI are pressed, now I want to fill them with code to actually create and start the threads.

  • What would be the correct procedure here?

  • Do I need to subclass NSThread?

  • Where do I implement the code, that the new threads will execute?

    I can't see a run method, or something similar. I suppose that has something to do with the selector, which I don't understand.

like image 808
user1809923 Avatar asked Nov 13 '12 16:11

user1809923


1 Answers

Use dispatch queues. They're essentially lightweight threads for which you don't need to worry about the threading or queueing directly.

-(void) spawn
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        [self doWorkInBackground];
    });
}

You can use one of the built-in queues or your own.

And you should probably read up on blocks too, in particular the memory management aspect.

like image 133
Kevin Avatar answered Sep 25 '22 21:09

Kevin