Search code examples
iosmacosthread-safetystatic-methodsnscalendar

How to ensure thread-safe use of "NSCalendar currentCalendar"?


According to Apple Docs NSCalendar is not thread-safe. How can I ensure thread-safety when I use the static method currentCalendar ?

Any library could call the same method. How can I lock the access?


Solution

  • You can use NSLock.

    NSLock *lock = [[NSLock alloc] init];
    

    and

    [lock lock];
    //calendar
    [lock unlock];