Doubts about concurrency with objects that can be used multiple times, e.g. formatting

Maybe a stupid question, but I need confirmation.

Usually, when I deal with objects that can be used several times in my application, I use this approach, as shown below.

Create an extension, for example NSDecimalNumber+Extension, or a utility of the class in which the formatting of numbers is created, as shown below.

+ (NSNumberFormatter*)internal_sharedNumberFormatter
{
    static NSNumberFormatter* _internal_numberFormatter = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _internal_numberFormatter = [[NSNumberFormatter alloc] init];
        // other configurations here...
    });

    return _internal_numberFormatter;
}

+ (NSString*)stringRepresentationOfDecimalNumber:(NSDecimalNumber*)numberToFormat
{
    NSString *stringRepresentation = [[self class] internal_sharedNumberFormatter] stringFromNumber:numberToFormat];
    return stringRepresentation;
}

This approach is very good because, for example, formatting is expensive to create. But this can be applied to other situations.

Now, my questions are as follows.

Is this approach also applicable in situations where a different execution path (different threads) is involved?

So, if I name the first one stringRepresentationOfDecimalNumberin the main thread, and then in the other thread, what could happen?

, stringRepresentationOfDecimalNumber , , .

.

+5
1

NSNumberFormatter , , , (. " ", ) .

, . , , , , . _internal_numberFormatter - , , .

+2

All Articles