Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C - Calling a class method on the main thread?

How can I call a CLASS METHOD on the main thread? Something like:

[SomeClass performSelectorOnMainThread:staticMethod withObject:nil];

Please don't tell me to create a regular method to call this class method. That would be an obvious solution, but not clean.

Thanks

like image 316
aryaxt Avatar asked Apr 12 '11 22:04

aryaxt


People also ask

What does @() mean in Objective-C?

In Objective-C, any character , numeric or boolean literal prefixed with the '@' character will evaluate to a pointer to an NSNumber object (In this case), initialized with that value. C's type suffixes may be used to control the size of numeric literals. '@' is used a lot in the objective-C world.

What is a MainThread?

When an application is launched in Android, it creates the first thread of execution, known as the “main” thread. The main thread is responsible for dispatching events to the appropriate user interface widgets as well as communicating with components from the Android UI toolkit.


1 Answers

[SomeClass performSelectorOnMainThread:staticMethod withObject:nil waitUntilDone:NO];

Yes, performSelectorOnMainThread:withObject:waitUntilDone: is not a class method.

Yes, it is an instance method on NSObject.

Yes, all Class objects are instances of NSObject. (I'm not kidding!)


You could also do:

dispatch_async(dispatch_get_main_queue(), ^{
  [SomeClass doClassyThingWithObject:object1 andDiddleyDoo:foo];
});
like image 120
Dave DeLong Avatar answered Sep 20 '22 20:09

Dave DeLong