Perfoming any Selector on the Main Thread

5 views
Skip to first unread message

Fredrik Olsson

unread,
Mar 30, 2010, 11:10:56 AM3/30/10
to CocoaHeads Øresund
It is quite easy to call a method on the main thread, from a
background thread, with Cocoa using the -[NSObject
performSelectorOnMainThread:withObject:waitUntilDone:] method.

But this method has a drawback, it takes max 1 argument, and that
argument must be an object. Making it not that useful if you want to
move a UI component using -[UIView setFrame:], since it takes a CGRect
that is a C struct as argument. Even worse for things like -
[UITableView deleteRowsForIndexPaths:withRowAnimation:], that takes
two arguments, the last one being a C primitive.

I have written a blog post, and releases the accompanying code as open
source, for a category on NSInvocation. A category that makes it just
as easy to call any method, on the main thread, regardless of the
number of arguments and/or their types.

http://blog.jayway.com/2010/03/30/performing-any-selector-on-the-main-thread/

regards
Fredrik Olsson

Reply all
Reply to author
Forward
0 new messages