Re: How to implement "while there are objects in the array - run runloop" idea?
Re: How to implement "while there are objects in the array - run runloop" idea?
- Subject: Re: How to implement "while there are objects in the array - run runloop" idea?
- From: "Stephen J. Butler" <email@hidden>
- Date: Thu, 26 May 2011 13:35:12 -0500
On Thu, May 26, 2011 at 12:56 PM, Nick <email@hidden> wrote:
> I have a custom (not main) thread, that adds objects to an NSArray at random
> times.
> I have an another thread (main thread) that has to retrieve these objects
> and do something with them (extract some properties from the retrieved
> object and make an NSURLConnection's asynchronous connection).
How are you synchronizing access to the shared array? Lock objects?
You might want to abandon that approach and not add objects from the
background thread directly to the shared array. Instead, you could
"signal" the main thread that a new object is ready to be processed.
Some common methods:
- Distributed Objects/NSConnection. The main thread would be your
server and your background thread the client. The client would call
something like [proxy processObject:blah] and the main thread would
perform the action.
- Use mach pipes to pass objects to the main thread. mach pipes can be
added as a source to the runloop. Either serialize and pass the object
over the pipe itself, or (since you are in the same address space)
just pass a token single byte message over the pipe to notify the main
thread that a new object has been added to the array to be processed.
- Use NSObject's performSelectorOnMainThread:withObject:waitUntilDone
to message the main thread directly from the background thread and
hand off the new object to be processed. Or you could still add the
object to the shared array and use
performSelectorOnMainThread:withObject:waitUntilDone to notify the
main thread.
- Use NSNotifications to notify that a new object is waiting to be
processed. If you go this route, you could actually have multiple
processing threads all listening for the notification.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden