I have some code which runs in a timer, but I would like to have a few of these run simultaneously.
How are threads run in objective c?
Can I put the current code in a method, and just start up threads and called the method in each thread?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The direct answer is yes: Use NSThread
The you can do something like this:
This will create a new Thread and call some method you define on some object. A common pitfall is that in a thread you need to create a separate NSAutoreleasePool if you’re not using Garbage Collection. In this case above it might look like this:
However, as others pointed out already, threads should be no longer used. They were sorta replaced by NSOperations, or Blocks and GrandCentralDispatch.