Call Class Method in background using Objective c
Asked Answered
C

3

12

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 ..

Cameron answered 28/11, 2012 at 4:37 Comment(0)
A
18

Just call

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

Because Classes are objects themselves, this will work.

Atbara answered 28/11, 2012 at 4:46 Comment(0)
P
2

please try self instead of class name

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

hope this wil work for you

Ptarmigan answered 28/11, 2012 at 6:36 Comment(0)
C
1

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.

Crumhorn answered 18/3, 2014 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.