Question

In the below excerpt,

/*A ClassName with instanceMethod and ClassMethod  */

-(void)instanceMethod;

+(void)ClassMethod;

/*To call a instance method in background */

ClassName  class1obj = [ClassName alloc] init];

[class1obj performSelectorInBackground:@selector(instanceMethod) withObject:nil];

Similarly, how to call a ClassMethod in background using performSelectorInBackground?

If possible, please explain! Please guys join hands ..

Was it helpful?

Solution

Just call

[ClassName performSelectorInBackground:@selector(ClassMethod) withObject:nil];

Because Classes are objects themselves, this will work.

OTHER TIPS

please try self instead of class name

 [self performSelectorInBackground:@selector(methodTobeCalled) withObject:nil];

hope this wil work for you

You should look at GCD (Grand Central Dispatch), which solves the general problem "How to execute code in the background". Doesn't matter whether it's calling a class method, calling an instance method, throwing dice, writing to a file, whatever.

Example:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSLog (@"This is code running in the background");
    [MyClass someClassMethod];
    [myInstance someMethodWithInt:1 bool:YES string:@"some string"];
    NSLog (@"Finished with the background code");
});

Works with arbitrary code; no need to use selectors, no need to write methods just to have a selector to run in the background, no need to turn parameters into NSObject (you can't use performSelector with an int or BOOL argument). Xcode will fill out most of this for you automatically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top