NSArray和NSDictionary是非线程安全的,所以YY添加了YYThreadSageArray和YYThreadSafeDictionary来添加一个锁,保证线程安全。
实现
采用了两个宏来添加信号量并发控制:
初始化和信号量创建宏:
#define INIT(...) self = super.init; \
if (!self) return nil; \
__VA_ARGS__; \
if (!_arr) return nil; \
_lock = dispatch_semaphore_create(1); \
return self;
添加信号量等待和信号量发送宏:
#define LOCK(...) dispatch_semaphore_wait(_lock, DISPATCH_TIME_FOREVER); \
__VA_ARGS__; \
dispatch_semaphore_signal(_lock);
一共使用的函数有:
- dispatch_semaphore_create
- dispatch_semaphore_wait
- dispatch_semaphore_signal
使用效果的解析在 YYWeaKProxy的学习中有说明。
NSArray及NSMutableArray需要重载保证线程安全的方法
初始化方法
- (instancetype)init
- (instancetype)initWithCapacity:(NSUInteger)numItems
- (instancetype)initWithArray:(NSArray *)array
- (instancetype)initWithObjects:(const id[])objects count:(NSUInteger)cnt
- (instancetype)initWithContentsOfFile:(NSString *)path
- (instancetype)initWithContentsOfURL:(NSURL *)url
实例方法
- (NSUInteger)count
- (id)objectAtIndex:(NSUInteger)index
- (NSArray *)arrayByAddingObject:(id)anObject
- (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray
- (NSString *)componentsJoinedByString:(NSString *)separator
- (BOOL)containsObject:(id)anObject
- (NSString *)description
- (NSString *)descriptionWithLocale:(id)locale
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level
- (id)firstObjectCommonWithArray:(NSArray *)otherArray
- (void)getObjects:(id __unsafe_unretained[])objects range:(NSRange)range
- (NSUInteger)indexOfObject:(id)anObject
- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range
- (NSUInteger)indexOfObjectIdenticalTo:(id)anObject
- (NSUInteger)indexOfObjectIdenticalTo:(id)anObject inRange:(NSRange)range
- (id)firstObject
- (id)lastObject
- (NSEnumerator *)objectEnumerator
- (NSEnumerator *)reverseObjectEnumerator
- (NSData *)sortedArrayHint
- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context
- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context hint:(NSData *)hint
- (NSArray *)sortedArrayUsingSelector:(SEL)comparator
- (NSArray *)subarrayWithRange:(NSRange)range
- (void)makeObjectsPerformSelector:(SEL)aSelector
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument
- (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes
- (id)objectAtIndexedSubscript:(NSUInteger)idx
- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block
- (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block
- (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate
- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate
- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate
- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate
- (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate
- (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate
- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr
- (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr
- (NSUInteger)indexOfObject:(id)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator)cmp
可变数组的实例方法
- (void)addObject:(id)anObject
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index
- (void)removeLastObject
- (void)removeObjectAtIndex:(NSUInteger)index
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject
- (void)addObjectsFromArray:(NSArray *)otherArray
- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2
- (void)removeAllObjects
- (void)removeObject:(id)anObject inRange:(NSRange)range
- (void)removeObject:(id)anObject
- (void)removeObjectIdenticalTo:(id)anObject inRange:(NSRange)range
- (void)removeObjectIdenticalTo:(id)anObject
- (void)removeObjectsInArray:(NSArray *)otherArray
- (void)removeObjectsInRange:(NSRange)range
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray
- (void)setArray:(NSArray *)otherArray
- (void)sortUsingFunction:(NSInteger (*)(id, id, void *))compare context:(void *)context
- (void)sortUsingSelector:(SEL)comparator
- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes
- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes
- (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx
- (void)sortUsingComparator:(NSComparator)cmptr
- (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr
- (BOOL)isEqualToArray:(NSArray *)otherArray
协议方法
- (id)copyWithZone:(NSZone *)zone
- (id)mutableCopyWithZone:(NSZone *)zone
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained[])stackbuf count:(NSUInteger)len
- (BOOL)isEqual:(id)object
- (NSUInteger)hash
NSDictionary及NSMutableDictionary需要重载保证线程安全的方法
初始化方法
- (instancetype)init
- (instancetype)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys
- (instancetype)initWithCapacity:(NSUInteger)capacity
- (instancetype)initWithObjects:(const id[])objects forKeys:(const id <NSCopying>[])keys count:(NSUInteger)cnt
- (instancetype)initWithDictionary:(NSDictionary *)otherDictionary
- (instancetype)initWithDictionary:(NSDictionary *)otherDictionary copyItems:(BOOL)flag
实例方法
- (NSUInteger)count
- (id)objectForKey:(id)aKey
- (NSEnumerator *)keyEnumerator
- (NSArray *)allKeys
- (NSArray *)allKeysForObject:(id)anObject
- (NSArray *)allValues
- (NSString *)description
- (NSString *)descriptionInStringsFileFormat
- (NSString *)descriptionWithLocale:(id)locale
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level
- (BOOL)isEqualToDictionary:(NSDictionary *)otherDictionary
- (NSEnumerator *)objectEnumerator
- (NSArray *)objectsForKeys:(NSArray *)keys notFoundMarker:(id)marker
- (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator
- (void)getObjects:(id __unsafe_unretained[])objects andKeys:(id __unsafe_unretained[])keys
- (id)objectForKeyedSubscript:(id)key
- (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key, id obj, BOOL *stop))block
- (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(id key, id obj, BOOL *stop))block
- (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptr
- (NSArray *)keysSortedByValueWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr
- (NSSet *)keysOfEntriesPassingTest:(BOOL (^)(id key, id obj, BOOL *stop))predicate
- (NSSet *)keysOfEntriesWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id key, id obj, BOOL *stop))predicate
可变字典
- (void)removeObjectForKey:(id)aKey
- (void)setObject:(id)anObject forKey:(id <NSCopying> )aKey
- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary
- (void)removeAllObjects
- (void)removeObjectsForKeys:(NSArray *)keyArray
- (void)setDictionary:(NSDictionary *)otherDictionary
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying> )key
协议方法
- (id)copyWithZone:(NSZone *)zone
- (id)mutableCopyWithZone:(NSZone *)zone
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained[])stackbuf count:(NSUInteger)len
- (BOOL)isEqual:(id)object
- (NSUInteger)hash
收获
了解到了NSArray和NSDictionary很多一直没有用到过的方法。学习到了NSIndexSet的用法,之前从来不会用到这个方法。都是自己编写逻辑写一遍。
// END
网友评论