Maybe a stupid question to ask but I need some confirmations on it.
Usually, when I deal with objects that can be used multiple times within my application I use an approach like the following.
Create an extension, say for example NSDecimalNumber+Extension
, or a class utility where a number formatter is created like the following.
+ (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 quite good since, for example, formatters are expensive to create. But it could be applied to other situations as well.
Now, my questions is the following.
Does this approach is also valid in situations where different path of execution (different threads) are involved?
So, if I call first stringRepresentationOfDecimalNumber
on the main thread and then in a different thread, what could happen?
I think is valid to perform different calls to stringRepresentationOfDecimalNumber
in different threads since the shared formatter, in this case, is reading only, but I would like to have a reply from experts.
Thanks in advance.