Class Methods and Thread Safety (NSJSONSerialization)

In my iOS application, I am doing some work in the background thread (using performSelectorInBackground ). In this thread, I use 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 of this? Where is this written in the documentation?

I know that instance methods are generally not thread safe unless they say so. Can I even say that class methods are thread safe, unless otherwise indicated?

+8
ios thread-safety objective-c cocoa
source share
3 answers

I have a little guess here.

Thread Programming Guide Report

Immutable objects are typically thread safe; once you create them, you can safely transfer these objects to and from threads.

Calling a class method means sending a message to a class object, and class objects are immutable. My conclusion would be that it is safe to call class methods from different hells.

+1
source share

I just posted a similar question on the Apple Developer Forum. The answer I received was that NSJSONSerialization is thread safe:

https://forums.developer.apple.com/thread/11229

+1
source share

Managed content is not thread safe. You can either run the code on your mainthread, or run it in the background, and use NSNotificationCenter to start reloading your user interface when processing the background thread.

-one
source share

All Articles