Question

In a Calendar-like app, or part of an app, I need to use (gregorian) calendar in many routines, which means I need to alloc-init and release the same NSCalendar object for many times. Shall I retain one NSCalendar (say gregorian calendar) as an ivar(@property(nonatomic,retain)) in my long persistent controller instance?

Or is its instance methods re-entrant/thread-safe?

Était-ce utile?

La solution

In general, you should keep calendars and date formatters around because they are expensive to create and/or use the first time (I found this out the hard way).

However, the Thread Safety Summary lists both NSCalendar and NSFormatter (NSDateFormatter's superclass) as “Thread-Unsafe”, saying:

The following classes and functions are generally not thread-safe. In most cases, you can use these classes from any thread as long as you use them from only one thread at a time. Check the class documentation for additional details.

I suggest looking for a way to do both. This is one advantage of creating threads yourself using NSThread: You can stash each thread's calendar in its thread dictionary.

Otherwise, you'll just have to create and throw away each calendar every time and pay the performance cost.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top