performSelector的具体用法

performSelector具体的创建方式有10种,根据执行方式分为了五大类。

//1️⃣三种 这三种跟直接调用方法没有区别,同步执行任务,阻塞当前线程。执行完sel再继续执行原来任务
[self performSelector:sel];
[self performSelector:sel withObject:nil];
[self performSelector:sel withObject:nil withObject:nil];

//2️⃣两种   延时执行、不阻塞当前线程
[self performSelector:sel withObject:nil afterDelay:1];
NSArray *array;
[self performSelector:sel withObject:nil afterDelay:1 inModes:array];

//3️⃣两种 子线程主线程均可调用,任务在主线程执行,主要用来更新UI
[self performSelectorOnMainThread:sel withObject:nil waitUntilDone:YES];
[self performSelectorOnMainThread:sel withObject:nil waitUntilDone:YES modes:array];

//4️⃣一种 开启子线程执行方法
[self performSelectorInBackground:sel withObject:nil];

//5️⃣两种  指定某个线程执行方法  YES:阻塞当前线程  NO:不阻塞当前线程
NSThread *thread;
[self performSelector:sel onThread:thread withObject:nil waitUntilDone:YES];
[self performSelector:sel onThread:thread withObject:nil waitUntilDone:YES modes:array];
复制代码

延时执行解决内存泄漏的方法:

执行 [self performSelector:sel withObject:self afterDelay:1]; 的时候,系统会将self的引用计数加1,执行完这个方法时,还会将self的引用计数减1,当方法还没有执行的时候,要返回父视图释放当前视图的时候,self的计数没有减少到0,而导致无法调用dealloc方法,出现了内存泄露。 所以最后我的解决办法就是取消那些还没有来得及执行的延时函数: [NSObject cancelPreviousPerformRequestsWithTarget:self] 当然你也可以一个一个得这样用: [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(method1:) object:nil]

1️⃣、正常执行方法,不阻塞当前线程
//三种 这三种跟直接调用方法没有区别,同步执行任务,阻塞当前线程。执行完sel再继续执行原来任务
[self performSelector:sel];
[self performSelector:sel withObject:nil];
[self performSelector:sel withObject:nil withObject:nil];
复制代码
- (void)threadTest{
    NSLog(@"1");
    SEL sel = @selector(getCurrentThread);
   [self performSelector:sel];
    NSLog(@"2");
}
- (void)getCurrentThread{
    NSThread *currentThread = [NSThread currentThread];
    NSLog(@"currentThread == %@",currentThread);
    NSLog(@"3");
}
复制代码
2018-06-21 14:58:30.168231+0800 WLZCyclycReference[27474:256398] 1
2018-06-21 14:58:30.168399+0800 WLZCyclycReference[27474:256398] currentThread == <NSThread: 0x60c00006a5c0>{number = 1, name = main}
2018-06-21 14:58:30.168481+0800 WLZCyclycReference[27474:256398] 3
2018-06-21 14:58:30.168573+0800 WLZCyclycReference[27474:256398] 2
复制代码
2️⃣、延时执行,异步执行,不阻塞当前线程。只能在主线程执行。
//两种   延时执行、不阻塞当前线程
[self performSelector:sel withObject:nil afterDelay:1];
NSArray *array;
[self performSelector:sel withObject:nil afterDelay:1 inModes:array];
复制代码
- (void)threadTest{
    NSLog(@"1");
    SEL sel = @selector(getCurrentThread);
   [self performSelector:sel withObject:nil afterDelay:1];
    NSLog(@"2");
}
- (void)getCurrentThread{
    NSThread *currentThread = [NSThread currentThread];
    NSLog(@"currentThread == %@",currentThread);
    NSLog(@"3");
}
复制代码
2018-06-21 15:01:28.122273+0800 WLZCyclycReference[27533:259144] 1
2018-06-21 15:01:28.122422+0800 WLZCyclycReference[27533:259144] 2
2018-06-21 15:01:29.122463+0800 WLZCyclycReference[27533:259144] currentThread == <NSThread: 0x604000071300>{number = 1, name = main}
2018-06-21 15:01:29.122664+0800 WLZCyclycReference[27533:259144] 3
复制代码
3️⃣、子线程主线程均可调用,任务在主线程执行,主要用来更新UI
//两种 在主线程执行  YES:阻塞主线程  NO:不阻塞主线程
[self performSelectorOnMainThread:sel withObject:nil waitUntilDone:YES];
[self performSelectorOnMainThread:sel withObject:nil waitUntilDone:YES modes:array];
复制代码
- (void)threadTest{
    NSLog(@"1");
    SEL sel = @selector(getCurrentThread);
    dispatch_queue_t queue = dispatch_queue_create("com.apple.www", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        [self performSelectorOnMainThread:sel withObject:nil waitUntilDone:YES];
    });
    NSLog(@"2");
}
- (void)getCurrentThread{
    //回到主线程更新UI
    NSLog(@"3");
}
复制代码

你期望的情况是不是 132,但是事实输出的是123,这是因为queue在开启子线程执行performSelectorOnMainThread 方法的过程中,NSLog(@"2");已经执行完了,如果想要得到理想的结果,那么你就要在主线程中放足够多的耗时任务,保证子线程执行到performSelectorOnMainThread方法的时候,主线程的任务还没执行完,酱紫才会出现阻塞主线程的情况。

4️⃣、开启子线程

这就没什么好说的了,开个子线程而已

//一种 开启子线程执行方法
[self performSelectorInBackground:sel withObject:nil];
复制代码
- (void)threadTest{
    NSLog(@"1");
    SEL sel = @selector(getCurrentThread);
    [self performSelectorInBackground:sel withObject:nil];
    NSLog(@"2");
}
- (void)getCurrentThread{
    NSThread *currentThread = [NSThread currentThread];
    NSLog(@"currentThread == %@",currentThread);
    NSLog(@"3");
}
2018-06-21 15:28:40.007851+0800 WLZCyclycReference[28121:284079] 1
2018-06-21 15:28:40.008061+0800 WLZCyclycReference[28121:284079] 2
2018-06-21 15:28:40.008270+0800 WLZCyclycReference[28121:284165] currentThread == <NSThread: 0x60c000078840>{number = 3, name = (null)}
2018-06-21 15:28:40.008428+0800 WLZCyclycReference[28121:284165] 3
复制代码
5️⃣、指定某个线程执行方法
//两种  指定某个线程执行方法  YES:阻塞当前线程  NO:不阻塞当前线程
NSThread *thread;
[self performSelector:sel onThread:thread withObject:nil waitUntilDone:YES];
[self performSelector:sel onThread:thread withObject:nil waitUntilDone:YES modes:array];
复制代码
- (void)threadTest{
    NSLog(@"1");
    SEL sel = @selector(getCurrentThread);
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(createThread) object:nil];
    [thread start];
    [self performSelector:sel onThread:thread withObject:nil waitUntilDone:NO];
    NSLog(@"2");
}
- (void)getCurrentThread{
    NSThread *currentThread = [NSThread currentThread];
    NSLog(@"currentThread == %@",currentThread);
    NSLog(@"3");
}
- (void)createThread{
    //创建线程记得开启runloop
    [[NSRunLoop currentRunLoop] run];
}
2018-06-21 15:52:08.413852+0800 WLZCyclycReference[28657:307146] 1
2018-06-21 15:52:08.414072+0800 WLZCyclycReference[28657:307146] 2
2018-06-21 15:52:08.414208+0800 WLZCyclycReference[28657:307230] currentThread == <NSThread: 0x6000002606c0>{number = 3, name = (null)}
2018-06-21 15:52:08.414307+0800 WLZCyclycReference[28657:307230] 3
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值