圖片來之網(wǎng)絡(luò)
NSArray和NSDictionary是非線程安全的,所以YY添加了YYThreadSageArray和YYThreadSafeDictionary來添加一個鎖,保證線程安全。
實現(xiàn)
采用了兩個宏來添加信號量并發(fā)控制:
初始化和信號量創(chuàng)建宏:
#define INIT(...) self = super.init; \
if (!self) return nil; \
__VA_ARGS__; \
if (!_arr) return nil; \
_lock = dispatch_semaphore_create(1); \
return self;
添加信號量等待和信號量發(fā)送宏:
#define LOCK(...) dispatch_semaphore_wait(_lock, DISPATCH_TIME_FOREVER); \
__VA_ARGS__; \
dispatch_semaphore_signal(_lock);
一共使用的函數(shù)有:
- dispatch_semaphore_create
- dispatch_semaphore_wait
- dispatch_semaphore_signal
使用效果的解析在 YYWeaKProxy的學(xué)習(xí)中有說明。
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
可變數(shù)組的實例方法
- (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
協(xié)議方法
- (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
協(xié)議方法
- (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很多一直沒有用到過的方法。學(xué)習(xí)到了NSIndexSet的用法,之前從來不會用到這個方法。都是自己編寫邏輯寫一遍。
// END