iOS-NSMutableArray+runtime交换方法

导读

当对一个可变数组操作很频繁,并且在多个线程操作同一个可变数组时,发生数组越界等奔溃是很常见的.所以使用runtime,对其方法进行交换.然后在交换方法中对增,删,改,查等做保护机制就可以避免类似情况发生.

源代码
1.新建一个NSMutableArray的分类

NSMutableArray+ SafeAccess.h文件声明

#import 

@interface NSMutableArray (SafeAccess)

@end
2.文件实现
// 需要导入runtime.h文件
#import 

然后在load方法中声明进行方法交换声明

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        id obj = [[self alloc] init];
        [obj swizzleMethod:@selector(addObject:) withMethod:@selector(safeAddObject:)];
        [obj swizzleMethod:@selector(objectAtIndex:) withMethod:@selector(safeObjectAtIndex:)];
        [obj swizzleMethod:@selector(removeObjectAtIndex:) withMethod:@selector(safeRemoveObjectAtIndex:)];
    });
}

- (void)swizzleMethod:(SEL)origSelector withMethod:(SEL)newSelector {
    Class cls = [self class];

    Method originalMethod = class_getInstanceMethod(cls, origSelector);
    Method swizzledMethod = class_getInstanceMethod(cls, newSelector);

    BOOL didAddMethod = class_addMethod(cls,
                                        origSelector,
                                        method_getImplementation(swizzledMethod),
                                        method_getTypeEncoding(swizzledMethod));
    if (didAddMethod) {
        class_replaceMethod(cls,
                            newSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

为什么在load方法中进行声明?

可以参考iOS开发+(void)load与+(void)initialize区别,里面做了很详细的介绍.

三个交换方法的实现

- (void)safeAddObject:(id)anObject {
    if (anObject) {
        [self safeAddObject:anObject];
    }else{
        NSLog(@"obj is nil");
    }
}

- (id)safeObjectAtIndex:(NSInteger)index {
    if(index<[self count]){
        return [self safeObjectAtIndex:index];
    }else{
        NSLog(@"index is beyond bounds ");
    }
    return nil;
}

- (void)safeRemoveObjectAtIndex:(NSUInteger)index {
    if (index >= [self count]) {
        return;
    }
    
    return [self safeRemoveObjectAtIndex:index];
}
3.测试用例
- (void)syncMutableArray {
    NSMutableArray *safeArr = [[NSMutableArray alloc] init];
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    for ( int i = 0; i < 1000; i ++) {
        dispatch_async(queue, ^{
            NSString *obj = [NSString stringWithFormat:@"%d",i];
            if (i % 9 == 0) {
                obj = nil;
            }
            NSLog(@"添加第%d个值%@",i,obj);
            [safeArr addObject:obj];
        });
        
        dispatch_async(queue, ^{
            NSLog(@"删除第%d个",i);
            [safeArr removeObjectAtIndex:i];
        });
        
        dispatch_async(queue, ^{
            NSLog(@"读取第%d个数据:%@",i,[safeArr objectAtIndex:i]);
        });
    }
}

运行结果如下:

1.gif
iOS-NSMutableArray+runtime交换方法_第1张图片
image.png

通过运行结果可知,当插入元素为空,删除元素索引越界,读取元素索引越界都被拦截了,避免了异常奔溃的发生。

4 发现函数 safeAddObject:调用多次问题

在函数safeAddObject:添加打印,发现程序一启动,该方法调用了多次(外界没有调用添加元素的方法)

    NSLog(@"input count = %d, anObject = %p",++num, &anObject);   //num 全局变量
    if (anObject) {
        [self safeAddObject:anObject];
    }else{
        NSLog(@"add obj is nil");
    }
}

程序一启动,发现该方法调用多次

iOS-NSMutableArray+runtime交换方法_第2张图片
image.png

然后外界插入空值

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self testSyncMutableArray];
}

- (void)testSyncMutableArray {
    NSString *obj = nil;
    
    NSMutableArray *array = [NSMutableArray array];
    NSLog(@"start obj = %p",&obj);
    [array addObject:obj];
    NSLog(@"end obj = %p, array = %@", &obj,array);
}

运行结果

iOS-NSMutableArray+runtime交换方法_第3张图片
image.png

通过打印结果可知,插入的对象并不是我们手动插入元素的对象,是系统调用api做的事情,因为我们将该类的插入元素的方法都交换了

项目连接地址

总结

结果运行良好,没有发生奔溃,因为即使多个线程同时操作一个可变数组,因为已经对其方法做了交换,在交换方法中已经做了包含措施,所以不会发生crash.

你可能感兴趣的:(iOS-NSMutableArray+runtime交换方法)