完全来自于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进制的形式输出内存地址,附加前缀0x
NSString* 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

iOS 多线程安全 与可变数组的更多相关文章

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

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

  2. iOS 多线程安全 与 可变字典

    这周最大的收获是稍稍通透了 多线程安全字典的重要性.  诱因是,发现了有字典坏地址错误      果断以为是 value 或者 key 是可能出现了空值,补充了潜在的判断,虽然有的位置已经预判断的,但 ...

  3. iOS -Swift 3.0 -Array(数组与可变数组相关属性及用法)

    // // ViewController.swift // Swift-Array // // Created by luorende on 16/9/12. // Copyright © 2016年 ...

  4. ios可变数组的所有操作

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

  5. iOS多线程编程指南

    iOS多线程编程指南(拓展篇)(1) 一.Cocoa 在Cocoa上面使用多线程的指南包括以下这些: (1)不可改变的对象一般是线程安全的.一旦你创建了它们,你可以把这些对象在线程间安全的传递.另一方 ...

  6. iOS多线程的初步研究(六)

    iOS多线程的初步研究(六) iOS平台提供更高级的并发(异步)调用接口,让你可以集中精力去设计需完成的任务代码,避免去写与程序逻辑无关的线程生成.运行等管理代码.当然实质上是这些接口隐含生成线程和管 ...

  7. iOS开发-OC语言 (四)数组

    知识点 1.NSArray 2.NSMutableArray 1.数组的基本用法: 2.数组的遍历 3.数组排序 ===========   NSArray  不可变数组  ============= ...

  8. iOS多线程开发之GCD(中篇)

    前文回顾: 上篇博客讲到GCD的实现是由队列和任务两部分组成,其中获取队列的方式有两种,第一种是通过GCD的API的dispatch_queue_create函数生成Dispatch Queue:第二 ...

  9. iOS 多线程 GCD part3:API

    https://www.jianshu.com/p/072111f5889d 2017.03.05 22:54* 字数 1667 阅读 88评论 0喜欢 1 0. 预备知识 GCD对时间的描述有些新奇 ...

随机推荐

  1. SQL Server Replace函数

    REPLACE用第三个表达式替换第一个字符串表达式中出现的所有第二个给定字符串表达式. 语法REPLACE ( 'string_expression1' , 'string_expression2' ...

  2. 嵌入式驱动开发之phy---fine Mac与Phy组成原理的简单分析

    关键字rj45.pci-e 1. general 下图是网口结构简图.网口由CPU.MAC和PHY三部分组成.DMA控制器通常属于CPU的一部分,用虚线放在这里是为了表示DMA控制器可能会参与到网口数 ...

  3. NLM算法

    non-Local Means 非局部均值 论文原文:http://www.ipol.im/pub/art/2011/bcm_nlm/?utm_source=doi 论文源代码:http://www. ...

  4. poj 3281(网络流+拆点)

    题目链接:http://poj.org/problem?id=3281 思路:设一个超级源点和一个超级汇点,源点与食物相连,饮料与汇点相连,然后就是对牛进行拆点,一边喜欢的食物相连,一边与喜欢的饮料相 ...

  5. PostgreSql Partition + Hibernate Insert

    与Oracle不同.PostgreSQL须要手动控制分区规则触发器. 步骤一:创建分区 CREATE TABLE table_partition_1( CHECK partition_column c ...

  6. boost::interprocess(2)

    //doc_anonymous_mutex_shared_data.hpp #include <boost/interprocess/sync/interprocess_mutex.hpp> ...

  7. poj3411

    Paid Roads Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6549   Accepted: 2427 Descri ...

  8. NavigationBar 背景颜色,字体颜色

    // 设置状态栏颜色 [application setStatusBarStyle:UIStatusBarStyleLightContent]; // 设置导航栏 [[UINavigationBar ...

  9. 初识yeoman

    最近开始新项目,在项目构建上面寻找合适的东西,grunt,bower到发现yeoman;学习了下,把一些东西记录下来然留着以后用. 1.什么是Yeoman Yeoman是Google的团队和外部贡献者 ...

  10. 170122、Netty 长连接服务

    推送服务 还记得一年半前,做的一个项目需要用到 Android 推送服务.和 iOS 不同,Android 生态中没有统一的推送服务.Google 虽然有 Google Cloud Messaging ...