当仓央嘉措遇上纳兰性德iOS 开发每天分享优质文章iOS Developer

iOS RunTime & RunLoop浅尝辄止(适合

2017-02-24  本文已影响133人  vision_colion

理论性的东西我就不介绍了,如果想了解详细的RunTime和RunLoop = > iOS runtime和runloop 传送门

首先申明,不是大牛,大牛勿喷!
RunTime和RunLoop并没想象的那么难

RunTimer

先建一个Person类备用
Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) NSInteger age;
    
+ (instancetype)person;

@end

Person.m

#import "Person.h"

@implementation Person
    
+ (instancetype)person{
    Person *person = [[self alloc] init];
    return person;
}
@end

为了测试方便,直接在ViewController测试
引入头文件#import <objc/runtime.h>
和#import <objc/message.h>
然后如下图

设置.png
Person *person = [Person person];
Class personClass = person.class;
unsigned int outCount = 0;

//遍历实例变量
Ivar *ivarPtr = class_copyIvarList(personClass, &outCount);
for (NSInteger i = 0; i < outCount; i ++) {
    Ivar ivar = ivarPtr[i];
    //ivar_getName(Ivar v)获取类的变量名
    NSLog(@"实例变量:%s",ivar_getName(ivar));
/*
打印结果:
实例变量:_name
实例变量:_age
*/
}
//遍历类中属性
objc_property_t *proertyPtr = class_copyPropertyList(personClass, &outCount);
for (NSInteger i = 0; i < outCount; i ++) {
    objc_property_t property = proertyPtr[i];
    //property_getName(<#objc_property_t property#>)获取类的属性名
    NSLog(@"属性:%s",property_getName(property));
/*
打印结果:
属性:name
属性:age
*/ 
}

//遍历对象方法
Method *methodPtr = class_copyMethodList(personClass, &outCount);
for (NSInteger i = 0; i < outCount; i ++) {
    Method method = methodPtr[i];
    //method_getName(<#Method m#>)获取类的方法名
    SEL selector = method_getName(method);
    NSLog(@"方法:%@",NSStringFromSelector(selector));
/*
打印结果:
方法:setAge:
方法:age
方法:.cxx_destruct
方法:name
方法:setName:
*/ 
}

//消息传递
objc_msgSend(person, @selector(setAge:),20);
NSLog(@"runtime:%ld",person.age);
/*
打印结果:runtime:20
*/

RunLoop

情景:刚入坑时候肯定遇到过在有定时器和UIScrollView
子类同时存在的情况下,滑动UIScrollView子类的时候定时器停止了。现在,莫慌,听我慢慢道来

RunLoop中系统默认注册了5个Mode(常用的是1,2,5):
1.NSDefaultRunLoopMode:App的默认Mode,通常主线程是在这个Mode下运行
2.UITrackingRunLoopMode:界面跟踪 Mode,用于 ScrollView 追踪触摸滑动,保证界面滑动时不受其他 Mode 影响
3.UIInitializationRunLoopMode: 在刚启动 App 时第进入的第一个 Mode,启动完成后就不再使用
4.GSEventReceiveRunLoopMode: 接受系统事件的内部 Mode,通常用不到
5.NSRunLoopCommonModes: 这是一个占位用的Mode,不是一种真正的Mode


NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];

//只适用于默认模式下,滚动的时候定时器停止
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

//适用于滚动模式下,但是不滚动的时候定时器会停止
[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];

//这种模式下能保证定时器一直走下去,会在滑动或不滑动的时候自动切换NSDefaultRunLoopMode 和 UITrackingRunLoopMode
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

RunLoop与多线程

1.NSThread篇

如下代码存在的问题:NSLog(@"222");不会执行,因为线程被回收了

NSThread *thread = [[NSThread alloc] initWithBlock:^{
        NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
        NSLog(@"111");
    }];
    [thread start];

- (void)timerMethod{
    [NSThread sleepForTimeInterval:1.0];//模拟耗时操作
    NSLog(@"222");
    static int a = 0;
    a ++;
    NSLog(@"%@%zi",[NSThread currentThread],a);
}

解决办法:
<一>:

NSThread *thread = [[NSThread alloc] initWithBlock:^{
        NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

        [[NSRunLoop currentRunLoop] run];//开启线程的RunLoop

        NSLog(@"111");
    }];
    [thread start];

<二>:加一个标识,例如我这边叫isFinished(BOOL类型)

某个操作处理self.isFinished = YES;
NSThread *thread = [[NSThread alloc] initWithBlock:^{
        NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

        //开启线程的RunLoop
        while (!self.finished) {
            [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.0001]];
        }

        NSLog(@"111");
    }];
    [thread start];

2.GCD篇

 
//创建timer
    dispatch_source_t timer *timer = = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));
    //设置timer
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    //回调
    dispatch_source_set_event_handler(timer, ^{
        NSLog(@"----%@----",[NSThread currentThread]);
    });
    //启动timer
    dispatch_resume(timer);
上一篇下一篇

猜你喜欢

热点阅读