Re: Invoking protocol method on the main thread
Re: Invoking protocol method on the main thread
- Subject: Re: Invoking protocol method on the main thread
- From: Greg Parker <email@hidden>
- Date: Thu, 15 Nov 2012 15:23:49 -0800
On Nov 15, 2012, at 3:17 PM, Graham Cox <email@hidden> wrote:
> I have a formal protocol for a notification callback I want to make. The protocol inherits the NSObject protocol.
>
> The caller accepts only objects conforming to this protocol as its delegate. When it's time to invoke the callback, I'd like to do so on the main thread, since the caller can be running on another thread. But -performSelectorOnMainThread:withObject:waitUntilDone: is not part of the NSObject protocol, it's part of the methods of NSObject, so my code won't compile because id<MyProtocol> objects don't include this method.
>
> How can I invoke a method of my protocol on the main thread?
>
>
> @protocol MyProtocol<NSObject>
>
> - (void) foo:(Caller*) caller;
>
> ...
>
>
> @interface Caller : NSObject
>
> @property (assign) id<MyProtocol> delegate;
>
> ...
>
>
>
> [self.delegate performSelectorOnMainThread:@selector(foo) withObject:self waitUntilDone:NO]; // method not found
The simple solution is to declare your property as NSObject<MyProtocol>* .
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
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