Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class methods & thread safety (NSJSONSerialization)

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?

like image 774
kuba Avatar asked Mar 19 '13 08:03

kuba


2 Answers

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.

like image 115
Martin R Avatar answered Sep 29 '22 16:09

Martin R


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

like image 36
Greg Brown Avatar answered Sep 29 '22 14:09

Greg Brown