Re: Delegation across thread
Re: Delegation across thread
- Subject: Re: Delegation across thread
- From: "Michael Ash" <email@hidden>
- Date: Fri, 31 Oct 2008 00:41:48 -0400
On Thu, Oct 30, 2008 at 1:52 PM, Andre Masse <email@hidden> wrote:
> Hi,
>
> I'm currently developing an "in house" application which serves as a front
> end to a PostgreSQL database. There will be only 4 users at maximum peak, so
> this is a fairly small load for the database. At this point, I'm using
> notifications to inform a particular form when a query result is ready to
> process. Since each form has its own connection (a cocoa wrapper class to
> libpq) I'm wondering if using delegation would be a better idea. Now, I may
> move the querying stuff to its own thread at some point (when I get more
> familiar to the cocoa way).
>
> I've read the net on how to implement a delegate in a custom class and it
> works when in the same thread but, is delegation working in a threading
> environment? In other words, can I call a delegate method from another
> thread?
It's important to remember that "delegate" is just a design pattern,
not a language feature. A delegate is just a defined set of messages
that you send to an object in certain situations. When it comes to
threads, they behave just like any other messages, because they are
just regular messages.
So what happens when you send a delegate message on a secondary
thread? Same thing as when you send any other message: the
corresponding method gets invoked synchronously on the same thread.
(I.e. you call it directly.)
This can be bad. If you're going to do this then your delegate needs
to be prepared to receive delegate messages on secondary threads, and
that can sometimes be annoying. Sometimes it's entirely reasonable. It
really depends on your situation. If your situation is such that it's
unreasonable, the solution is easy: just use
performSelectorOnMainThread: to ship the delegate message back to the
main thread where it can be invoked in a better place. If you want to
invoke it on some thread other than the main thread then Cocoa
provides a fair number of inter-thread communication techniques that
you can use. Ignore the fact that this is a "delegate" and just see
how and where you want to send your message.
Mike
_______________________________________________
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