成熟丰满熟妇高潮XXXXX,人妻无码AV中文系列久久兔费 ,国产精品一国产精品,国精品午夜福利视频不卡麻豆

您好,歡迎來(lái)到九壹網(wǎng)。
搜索
您的當(dāng)前位置:首頁(yè)iOS 多線(xiàn)程安全 與可變數(shù)組

iOS 多線(xiàn)程安全 與可變數(shù)組

來(lái)源:九壹網(wǎng)

完全來(lái)自于?的學(xué)習(xí)

基本相同,舉一反三

直接上樣例代碼

是我參照網(wǎng)上,根據(jù)當(dāng)前業(yè)務(wù)需求改的。

其實(shí)好多人在這里喜歡用類(lèi)別處理。我個(gè)人覺(jué)得用類(lèi)別 極其容易和普通方法混淆,所以為了降低耦合度,增強(qiáng)代碼理解性和可讀性。這里單獨(dú)創(chuàng)建類(lèi)挺好的。用時(shí)候使用這個(gè)自定義的安全數(shù)組就好了。

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

#import <Foundation/Foundation.h>

@interface SyncMutableArray : NSObject

//只讀
- (NSMutableArray *)safeArray;

//判斷是否包含對(duì)象
- (BOOL)containsObject:(id)anObject;

//集合元素?cái)?shù)量
- (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進(jìn)制的形式輸出內(nèi)存地址,附加前綴0x
        NSString* uuid = [NSString stringWithFormat:@"com.huofar.array_%p", self];
        //注意:_syncQueue是并行隊(duì)列
        _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 - 數(shù)據(jù)操作方法 (凡涉及更改數(shù)組中元素的操作,使用異步派發(fā)+柵欄塊;讀取數(shù)據(jù)使用 同步派發(fā)+并行隊(duì)列)

- (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:///zhang522802884/article/details/767202

轉(zhuǎn)載于:https://www.cnblogs.com/someonelikeyou/p/9151688.html

因篇幅問(wèn)題不能全部顯示,請(qǐng)點(diǎn)此查看更多更全內(nèi)容

Copyright ? 2019- 91gzw.com 版權(quán)所有 湘ICP備2023023988號(hào)-2

違法及侵權(quán)請(qǐng)聯(lián)系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市萬(wàn)商天勤律師事務(wù)所王興未律師提供法律服務(wù)