NSMutableDictionary thread safety

后端 未结 5 1771
不知归路
不知归路 2020-11-28 03:48

I have a question on thread safety while using NSMutableDictionary.

The main thread is reading data from NSMutableDictionary where:

5条回答
  •  一整个雨季
    2020-11-28 04:05

    Nowadays you'd probably go for @synchronized(object) instead.

    ...
    @synchronized(dictionary) {
        [dictionary setObject:image forKey:name];
    }
    ...
    @synchronized(dictionary) {
        [dictionary objectForKey:key];
    }
    ...
    @synchronized(dictionary) {
        [dictionary removeObjectForKey:key];
    }
    

    No need for the NSLock object any more

提交回复
热议问题