NSMutableDictionary thread safety

后端 未结 5 1777
不知归路
不知归路 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:17

    I have two options to using nsmutabledictionary.

    One is:

    NSLock* lock = [[NSLock alloc] init];
    [lock lock];
    [object.dictionary setObject:image forKey:name];
    [lock unlock];
    

    Two is:

    //Let's assume var image, name are setup properly
    dispatch_async(dispatch_get_main_queue(), 
    ^{ 
            [object.dictionary setObject:image forKey:name];
    });
    

    I dont know why some people want to overwrite setting and getting of mutabledictionary.

提交回复
热议问题