完全来自于 iOS 多线程安全与可变字典 的学习

基本相同,举一反三

直接上样例代码

是我参照网上,根据当前业务需求改的。

其实好多人在这里喜欢用类别处理。我个人觉得用类别 极其容易和普通方法混淆,所以为了降低耦合度,增强代码理解性和可读性。这里单独创建类挺好的。用时候使用这个自定义的安全数组就好了。

//  MensesTracker
//
//  Created by HF on 2018/6/7.
//  Copyright © 2018年 huofar. All rights reserved.
//

#import <Foundation/Foundation.h>@interface SyncMutableArray : NSObject//只读
- (NSMutableArray *)safeArray;//判断是否包含对象
- (BOOL)containsObject:(id)anObject;//集合元素数量
- (NSUInteger)count;//获取元素
- (id)objectAtIndex:(NSUInteger)index;
//枚举元素
- (NSEnumerator *)objectEnumerator;
//插入
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
//插入
- (void)addObject:(id)anObject;
//移除
- (void)removeObjectAtIndex:(NSUInteger)index;
//移除
- (void)removeObject:(id)anObject;
//移除
- (void)removeLastObject;
//替换
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
//获取索引
- (NSUInteger)indexOfObject:(id)anObject;@end

//
//  SyncMutableArray.m
//  MensesTracker
//
//  Created by HF on 2018/6/7.
//  Copyright © 2018年 huofar. All rights reserved.
//

#import "SyncMutableArray.h"@interface SyncMutableArray ()@property (nonatomic, strong) dispatch_queue_t syncQueue;
@property (nonatomic, strong) NSMutableArray* array;@end@implementation SyncMutableArray#pragma mark - init 方法
- (instancetype)initCommon
{self = [super init];if (self) {//%p 以16进制的形式输出内存地址,附加前缀0xNSString* uuid = [NSString stringWithFormat:@"com.huofar.array_%p", self];//注意:_syncQueue是并行队列_syncQueue = dispatch_queue_create([uuid UTF8String], DISPATCH_QUEUE_CONCURRENT);}return self;
}- (instancetype)init
{self = [self initCommon];if (self) {_array = [NSMutableArray array];}return self;
}//其他init方法略

#pragma mark - 数据操作方法 (凡涉及更改数组中元素的操作,使用异步派发+栅栏块;读取数据使用 同步派发+并行队列)- (NSMutableArray *)safeArray
{__block NSMutableArray *safeArray;dispatch_sync(_syncQueue, ^{safeArray = _array;});return safeArray;
}- (BOOL)containsObject:(id)anObject
{__block BOOL isExist = NO;dispatch_sync(_syncQueue, ^{isExist = [_array containsObject:anObject];});return isExist;
}- (NSUInteger)count
{__block NSUInteger count;dispatch_sync(_syncQueue, ^{count = _array.count;});return count;
}- (id)objectAtIndex:(NSUInteger)index
{__block id obj;dispatch_sync(_syncQueue, ^{if (index < [_array count]) {obj = _array[index];}});return obj;
}- (NSEnumerator *)objectEnumerator
{__block NSEnumerator *enu;dispatch_sync(_syncQueue, ^{enu = [_array objectEnumerator];});return enu;
}- (void)insertObject:(id)anObject atIndex:(NSUInteger)index
{dispatch_barrier_async(_syncQueue, ^{if (anObject && index < [_array count]) {[_array insertObject:anObject atIndex:index];}});
}- (void)addObject:(id)anObject
{dispatch_barrier_async(_syncQueue, ^{if(anObject){[_array addObject:anObject];}});
}- (void)removeObjectAtIndex:(NSUInteger)index
{dispatch_barrier_async(_syncQueue, ^{if (index < [_array count]) {[_array removeObjectAtIndex:index];}});
}- (void)removeObject:(id)anObject
{dispatch_barrier_async(_syncQueue, ^{[_array removeObject:anObject];//外边自己判断合法性
    });
}- (void)removeLastObject
{dispatch_barrier_async(_syncQueue, ^{[_array removeLastObject];});
}- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject
{dispatch_barrier_async(_syncQueue, ^{if (anObject && index < [_array count]) {[_array replaceObjectAtIndex:index withObject:anObject];}});
}- (NSUInteger)indexOfObject:(id)anObject
{__block NSUInteger index = NSNotFound;dispatch_sync(_syncQueue, ^{for (int i = 0; i < [_array count]; i ++) {if ([_array objectAtIndex:i] == anObject) {index = i;break;}}});return index;
}- (void)dealloc
{if (_syncQueue) {_syncQueue = NULL;}
}@end

参考

1. https://www.aliyun.com/jiaocheng/354967.html

2.https://blog.csdn.net/zhang522802884/article/details/76728902

转载于:https://www.cnblogs.com/someonelikeyou/p/9151688.html

iOS 多线程安全 与可变数组相关推荐

  1. IOS 中runtime 不可变数组__NSArray0 和__NSArrayI

    IOS 中runtime 不可变数组__NSArray0 和__NSArrayI 大家可能都遇到过项目中不可变数组避免数组越界的处理:runtime,然而有时候并不能解决所有的问题,因为类簇不一样 # ...

  2. iOS开发:不可变数组和可变数组的区别分析(NSArray / NSMutableArray)

    本篇博文分享一个理论知识点,Object-C中可变数组和不可变数组的对比使用,知识比较简单基础,大牛可以忽略.在iOS开发中,一般经常用NSArray类和NSMutableArray类来表示数组,其中 ...

  3. 浅谈一种规避iOS多线程Crash的方案

    一般来说,多线程编程因具有以下几个优点,一直被广泛应用: 资源利用率更好 程序设计在某些情况下更简单 程序响应更快 但是因为多线程而导致的crash问题,也是令程序员非常头疼的一个问题,因为线程调度执 ...

  4. ios 替换数组中元素_ios可变数组的所有操作

    #pragma mark 创建数组c NSMutableArray * array =[[NSMutableArray alloc] initWithObjects:@"a",@& ...

  5. ios可变数组的操作

    #pragma mark 创建数组c NSMutableArray * array =[[NSMutableArray alloc] initWithObjects:@"a",@& ...

  6. iOS多线程:『NSOperation、NSOperationQueue』详尽总结

    2019独角兽企业重金招聘Python工程师标准>>> iOS多线程:『NSOperation.NSOperationQueue』详尽总结 转载: 原地址https://www.ji ...

  7. IOS 多线程04-GCD详解 底层并发 API

    IOS 多线程04-GCD详解 底层并发 API 注:本人是翻译过来,并且加上本人的一点见解. 前言 想要揭示出表面之下深层次的一些可利用的方面.这些底层的 API 提供了大量的灵活性,随之而来的是大 ...

  8. iOS多线程详解:实践篇

    iOS多线程实践中,常用的就是子线程执行耗时操作,然后回到主线程刷新UI.在iOS中每个进程启动后都会建立一个主线程(UI线程),这个线程是其他线程的父线程.由于在iOS中除了主线程,其他子线程是独立 ...

  9. iOS 多线程和GCD(Grand Central Dispath) 教程 (一)

    iOS 多线程和GCD(Grand Central Dispath) 教程 (一)  本文翻译自 Ray Wenderlich 的博客 点击打开原文链接.全部由本人亲手翻译...童叟无欺~ 你有木有遇 ...

最新文章

  1. Image Filters for IOS
  2. 哈佛图书馆墙上的训言
  3. Keras实现mode.fit和model.fit_generator比较
  4. AFNetworking 3.0 发送soap到webservice
  5. python中浅拷贝和深拷贝分析
  6. Sentinel配置规则持久化
  7. 秋季唯美海报,打造的一系列秋季主题视觉
  8. Log4Net 之走进Log4Net (四)
  9. data fastboot 擦除_fastboot命令大全
  10. IP的正则表达式 IP地址的正则表达式写法
  11. 源地址转换、目的地址装换
  12. 关于知识分享和微软TechEd Roadshow
  13. 中国月球探测标识确定 寓龙的传人登月梦
  14. java队列打印杨辉三角_数组打印杨辉三角与队列打印杨辉三角
  15. 【Codeforces 372A】Counting Kangaroos is Fun
  16. Struts2文件的下载
  17. 插入U盘后 计算机未响应,电脑插入U盘后没有反应怎么办?
  18. IE浏览器版本测试方法
  19. linux中浏览器连不上网络,在Linux中修复Firefox浏览器“Network Protocol Error”错误的方法...
  20. 梯度下降法的简单理解

热门文章

  1. 二关节机械臂matlab控制,二连杆机械臂阻抗控制模拟(一)
  2. C语言课程设计——N-S图
  3. 把Excel当成数据库操作
  4. 黑马程序员_java开发前缀
  5. 喜事一桩|派盾科技荣获2020年杭州高新区“5050计划”政策扶持
  6. 用TreeView做权限导航的一个例子
  7. 微信分享网站链接带缩略图和描述
  8. gitEE(码云)的使用
  9. 100个网站推广方法
  10. windows重装系统后重用之前安装的oracle配置(转)