Singleton style in the main theme - iPhone

My question may be stupid, but I do not understand this. I am creating a singleton class using this code.

+ (GameRequestHandler *) sharedInstance
{
    static dispatch_once_t pred;
    static GameRequestHandler *shared = nil;

    dispatch_once(&pred, ^{
        shared = [[GameRequestHandler alloc] init];
    });
    return shared;
}

When I call methods from this singleton object, are they called in the main thread or in the background thread?

+3
source share
2 answers

Methods are called on the thread from which you are calling.

dispatch_onceit simply ensures that the block passed to it is executed once during the entire life of the application. I don't think it uses threads, and if so, then this is an implementation detail that you need not worry about.

+7
source

if you need a call in the main thread use this

dispatch_once(&pred, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
                    shared = [[GameRequestHandler alloc] init];
        });
    });
-1
source

All Articles