Re: Mixing Obj-C and C "methods"
Re: Mixing Obj-C and C "methods"
- Subject: Re: Mixing Obj-C and C "methods"
- From: Quincey Morris <email@hidden>
- Date: Tue, 30 Jul 2013 10:03:21 -0700
On Jul 30, 2013, at 08:54 , Vincent Habchi <email@hidden> wrote:
> The way SQLite works, after a query is submitted, the thread is suspended and a callback is repeatedly executed
There's one other consideration, in the case when the callback is called a thread other than the main thread, from non-ObjC code. You should also ensure that there's an autorelease pool in place, especially when using ARC (which may invoke 'autorelease' at times you can't easily predict).
Thus, I would advocate an approach based on the one Jean-Daniel mentioned earlier in the thread: move the code that does the actual work needed in the callback to a separate *method*, but hide your housekeeping -- the ugly bits -- in the function itself:
static int MyCallback (void* context) { // assuming the parameter is just the object pointer
@autoreleasepool {
int result = [(__bridge MyClass*) context myCallbackMethod];
return result;
}
}
In the case where the context is actually a structure containing the object pointer, then I'd retrieve it in this function (and likely all the other structure members, to pass as arguments to the method, unless there are an awful lot of them) so that the method can be written cleanly using normal ObjC conventions.
_______________________________________________
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