iosobjective-cobjective-c-category

How dispatch_once works in a category in objective c?


Hello i am new to objective c. I created a category in objective c. I used dispatch_once to define the Custom UIColor.

Check the code below.

#import "UIColor+CustomColors.h"

@implementation UIColor (CustomColors)

- (UIColor *)GetCustomColorLightYellowBGColor {

    static UIColor *lightYellowBGColor;
    
    //use dispatch_once_t pointer predicate to check if the block has finished
    //A pointer to a dispatch_once_t structure that is used to test whether the block has completed or not
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        lightYellowBGColor = [UIColor colorWithRed:244.0 / 255.0
                                           green:230.0 / 255.0
                                            blue:54.0 / 255.0
                                           alpha:1.0];
    });

    return lightYellowBGColor;
}

@end

dispatch_once documentation at https://developer.apple.com/documentation/dispatch/1447169-dispatch_once mentions the following

This function is useful for initialization of global data (singletons) in an application. Always call this function before using or testing any variables that are initialized by the block.

So i initialize the Custom UIColor Class and then i use the

self.view.backgroundColor = customColorClass.CustomColorLightYellowBGColor;

Color changes correctly on the view so everything is ok.

I want to ask the following.

If i use again on purpose self.view.backgroundColor = customColorClass.GetCustomColorLightYellowBGColor;

Will dispatch_once initialise the UIColor again or because is a singleton initialisation will not initialise the UIColor? Any help appreciated.


Solution

  • dispatch_once() works the same no matter what compilation unit it is in. So, yes, it will initialize lightYellowBGColor exactly once.

    Note that the method name GetCustomColorLightYellowBGColor really should be just customColorLightYellowBGColor and it should be a class method, not an instance method, as you don't need to instantiate a UIColor to have the method work.

    Better, it shouldn't be a category on UIColor at all. While convenient, categories lead to messy code because you are effectively interleaving your app specific code with the framework code.

    A typical pattern for this sort of thing would be to have a MyAppConfiguration class or something that contains a bunch of methods for retrieving configuration items like this. This also lays the easy foundation for allowing the user to customize the app's configuration and/or presentation by pooling all the configurable items in one place.