In my iOS app, I am doing some work in a background thread (using performSelectorInBackground
). In this thread, I am using NSJSONSerialization and its class methods to parse a JSON string:
self.json = [NSJSONSerialization JSONObjectWithData:self.data options:0 error:nil];
Is this class method (JSONObjectWithData
) thread safe? Can I be sure about this? Where is it written in the documentation?
I know that instance methods are generally not thread-safe unless the docs say they are. Can I generally say that class methods are thread-safe, unless specified otherwise?
I am a little bit guessing here.
The Threading Programming Guide states
Immutable objects are generally thread-safe; once you create them, you can safely pass these objects to and from threads.
Calling a class method means sending a message to the class object, and class objects are immutable. My conclusion would be that it is safe to call class methods from different theads.
I just posted a similar question on the Apple developer forum. The response I got was that NSJSONSerialization is thread-safe:
https://forums.developer.apple.com/thread/11229
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With