美文网首页iOS基础基础
performSelector和cancelPerform的妙用

performSelector和cancelPerform的妙用

作者: 妖精的尾巴毛 | 来源:发表于2020-08-14 13:50 被阅读0次

performSelector配合cancelPreviousPerformRequestsWithTarget真是妙用无穷,有如槟榔加烟的功效。

俩API长这样:(不认识的童鞋建议转行哈,建议卖黄焖鸡。)

-(void)performSelector:(SEL)aSelector
            withObject:(nullable id)anArgument
            afterDelay:(NSTimeInterval)delay

- (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget
                                       selector:(SEL)aSelector
                                         object:(nullable id)anArgument;

1、需求背景:比如直播间支持让用户一直点赞,总不能用户点一次赞我们代码调一次接口,在并发100W的直播很可能被用户点挂掉。所以产品要求每隔3秒上传一次3秒内的累计点赞数。

实现方法很多,NSTimer,GCD的After什么的都可以,这里说用
performSelector配合cancelPreviousPerformRequestsWithTarget来简单实现:

模拟点击事件代码:

 UIButton *  button = [UIButton buttonWithType:0];
    button.frame = CGRectMake(100, 180, 100, 100);
    button.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonClickTwo) forControlEvents:1<<6];

- (void)buttonClickTwo{
    [self performSelector:@selector(sayHelloTwo) withObject:nil afterDelay:3];
}

- (void)sayHelloTwo{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(sayHelloTwo) object:nil];
    NSLog(@"hello");//这里写上传点赞代码
}

刚开始sayHelloTwo先调用,cancelPreviousPerformRequestsWithTarget 调用的时候sayHelloTwo已经调用了,之后cancelPreviousPerform会取消3秒内的request ,结果就是hello 会每隔3秒打印一次,达到需求要求说明实现木有问题。

2、需求背景:比如直播主页也支持让用户一直点赞,也不能用户点一次赞我们代码调一次接口,因为没必要,这里不要求数据那么及时。所以要求用户点赞结束之后再调接口上传给服务器。

那么问题来了,怎么算结束?基本可以认为用户点赞0.5秒之后没再继续点,就算结束。当然0.5可以商议,举个栗子而已。

模拟点击事件代码:

 UIButton *  button = [UIButton buttonWithType:0];
    button.frame = CGRectMake(100, 180, 100, 100);
    button.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:1<<6];

- (void)buttonClick{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(sayHello) object:nil];
    [self performSelector:@selector(sayHello) withObject:nil afterDelay:0.5];
}

- (void)sayHello{
    NSLog(@"hello");//这里写上传点赞代码
}

可以看到,每次延迟调用sayHello之前都取消前一个还没调的request。结果就是只要每次点赞操作间隔在0.5秒之内,都算连续的。间隔超过0.5秒将算结束,将调用sayHello。

cancelPreviousPerformRequestsWithTarget 写在了不同的位置,产生完全不同的结果。

ps:有滚动事件时得注意performSelector的mode问题。

相关文章

网友评论

    本文标题:performSelector和cancelPerform的妙用

    本文链接:https://www.haomeiwen.com/subject/xcfgdktx.html