/**
线程安全的字典
*/
@interface OSSSyncMutableDictionary : NSObject
@property (nonatomic, strong) NSMutableDictionary *dictionary;
@property (nonatomic, strong) dispatch_queue_t dispatchQueue;
- (id)objectForKey:(id)aKey;
- (NSArray *)allKeys;
- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
- (void)removeObjectForKey:(id)aKey;
@end
@implementation OSSSyncMutableDictionary
- (instancetype)init {
if (self = [super init]) {
_dictionary = [NSMutableDictionary new];
_dispatchQueue = dispatch_queue_create("com.aliyun.aliyunsycmutabledictionary", DISPATCH_QUEUE_SERIAL);
}
return self;
}
- (NSArray *)allKeys {
__block NSArray *allKeys = nil;
dispatch_sync(self.dispatchQueue, ^{
allKeys = [self.dictionary allKeys];
});
return allKeys;
}
- (id)objectForKey:(id)aKey {
__block id returnObject = nil;
dispatch_sync(self.dispatchQueue, ^{
returnObject = [self.dictionary objectForKey:aKey];
});
return returnObject;
}
- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey {
dispatch_sync(self.dispatchQueue, ^{
[self.dictionary setObject:anObject forKey:aKey];
});
}
- (void)removeObjectForKey:(id)aKey {
dispatch_sync(self.dispatchQueue, ^{
[self.dictionary removeObjectForKey:aKey];
});
}
@end
看不懂的话,请留言我!!!
网友评论
@property (nonatomic, strong) dispatch_queue_t sessionQueue;
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{ //+++<OS_dispatch_queue: session queue[0x1741702c0]>+++
NSLog(@"+++%@+++",[self sessionQueue]);
dispatch_async([self sessionQueue], ^{
NSLog(@"666");
});
}
我这么写会崩 如果我传入主队列 和全局并发队列是可以的 但是我直接调用属性的get方法不成