美文网首页
SDWebimage 和Runloop

SDWebimage 和Runloop

作者: 七里田间的守望者 | 来源:发表于2016-11-26 11:22 被阅读159次

SDWebimage

SDWebimage的缓存周期是默认是一个星期

#内存警告的时候移除内存中的图片


- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    //清除内存缓存
    [[SDWebImageManager sharedManager].imageCache clearMemory];

    //取消所有图片下载
    [[SDWebImageManager sharedManager]cancelAll];
}


//直接下载图片  可以给UIButton添加Button分类设置图片
    [[SDWebImageManager sharedManager]downloadImageWithURL:[NSURL URLWithString:app.icon] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        
    }];

//cell下载图片的多个参数
 [cell.imageView sd_setImageWithURL:[NSURL URLWithString:app.icon] placeholderImage:[UIImage imageNamed:@"timg.jpeg"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        //expectedSize:图片的总字数
        //receivedSize:已经接收的图片
        
        NSLog(@"当前图片下载进度:%f",1.0 * receivedSize/expectedSize);
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        //图片下载完成
        
    }];

Runloop

  • runloop 简介:字面意思运行循环

  • 作用:

    • 保持程序的持续运行
    • 处理APP的各种事件(触摸、定时器、等)
    • 节省CPU资源 提高程序性能:该做事时做事,不该做事时休息
  • runloop 和线程

    • 每个线程都有唯一的一个与之对应的runloop对象
    • 主线程的runloop已经自动创建好了,子线程的runloop需要手动创建
    • runloop在第一次获取时创建,在线程结束时销毁

[NSRunLoop currentRunLoop];//当前线程的runloop
[NSRunLoop mainRunLoop];//主线程的runloop
CFRunLoopGetMain();//获得主线程的runloop
CFRunLoopGetCurrent();//获得当前线程的runloop

###NSTimer
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];//默认就已经加入到runloop中了
    
NSTimer * timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];//不会自动加入到runloop中 需要手动添加到runloop  mode为NSDefaultRunLoopMode
    
[[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
    
[[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
    
//NSRunLoopCommonModes下 一旦runloop进入其他模式 定时器就不会工作了
//NSDefaultRunLoopMode下 定时器会跑在标记common modes模式下
//标记common modes的模式:NSDefaultRunLoopMode 和 UITrackingRunLoopMode


###source 事件源

  - source0:非基于port的
  - source1:基于port,通过内核和其他线程通讯,接受分发系统事件


###observer 观察者 能够监听runloop的状态改变

//添加observer
CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(CFAllocatorGetDefault(), kCFRunLoopAllActivities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
        
        NSLog(@"监听到runloop状态改变---%zd",activity);
        
    });
    
    //添加观察者:监听runloop的状态
CFRunLoopAddObserver(CFRunLoopGetCurrent(), observer, kCFRunLoopDefaultMode);

runloop处理逻辑

Paste_Image.png
###常驻线程实现方法
self.thread = [[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];
[self.thread start];

- (void)run
{
    //常驻线程  可以保证线程不死
    
    //添加source  保证runloop不退出
    [[NSRunLoop currentRunLoop]addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
    //启动runloop
    [[NSRunLoop currentRunLoop]run];
}

###在子线程中添加timer

- (void)run
{

@autoreleasepool {//子线程里面添加runloop时 最好用释放池包住
        //在子线程中添加timer
        NSTimer * timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(test) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
        //启动runloop
        [[NSRunLoop currentRunLoop]run];
    }
}

- (void)test
{
    NSLog(@"testtesttesttest");
}

什么是runloop?

  • 字面意思看:运行循环 跑圈
  • 其实它内部是do - while循环,在这个循环内部不断处理各种任务(timer、source、observe)
  • 一个线程对应一个runloop
  • 主线程runloop默认自己启动 子线程的runloop得手动启动(调用run方法)
  • runloop只能选择一个mode启动,如果mode中没有任何source、timer、observe 的话runloop会自动退出

你再开发过程中怎么使用runloop?什么应用场景?

  • 开启一个常驻线程(让子线程不进入消亡状态,等待其他线程发来的消息,处理其他事件)

    • 在子线程添加一个定时器
    • 在子线程中进行长期监控一些行为
  • 可以让定时器在特定模式下运行

  • 可以让某些事件在特定模式下执行

  • 可以添加observe监听runloop的状态

自动释放池什么时候释放?

  • 在runloop即将睡眠的时候释放

GCD定时器(不受runloop的影响)


int count = 0;
//获得队列
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

//创建一个定时器  dispatch_source_t本质还是OC对象  需要强引用
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);

//设置定时器的各种属性 (几时开始 每隔多长时间执行一次)
//GCD的时间参数一般是纳秒 一秒等于10的9次方纳秒
//比当前时间晚3秒
//dispatch_time(DISPATCH_TIME_NOW, 3.0 * NSEC_PER_SEC);

dispatch_time_t interval = 2.0 * NSEC_PER_SEC;
dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, interval, 0);

//设置回调
dispatch_source_set_event_handler(self.timer, ^{
   
    NSLog(@"%@",[NSThread currentThread]);
    
    count ++;
    
    if (count == 4) {
        //取消定时器
        dispatch_cancel(self.timer);
        self.timer = nil;
    }
    
});

//启动定时器
dispatch_resume(self.timer);

相关文章

  • 05-多线程(3)

    0710SDWebImage和RunLoop 1、SDWebImage常用API(02-sdwebimage) ...

  • SDWebimage 和Runloop

    SDWebimage SDWebimage的缓存周期是默认是一个星期 Runloop runloop 简介:字面意...

  • iOS面试中经常遇到的问题二

    1、Runtime消息转发机制? 2、Runloop 让Runloop保活的方式? 3、SDWebImage的缓存...

  • iOS面试小集

    1、SDWebImage原理2、什么是Block?3、RunLoop剖析 一、 SDWebImage原理 一个为U...

  • 多线程网络-3

    Xmind PPT NSOperation RunLoop GitBook 0.第三方框架SDWebImage (...

  • 2022-09-20

    Runloop runtime kvo kvo多线程sdwebimage afnetwork底层原理内存管理定时器...

  • RunLoop

    基本概念RunLoop相关类RunLoop的作用RunLoop和线程的关系RunLoop和autoreleasep...

  • iOS 多线程:RunLoop详细总结

    RunLoop 文章目录RunLoop 简介1.1 什么是 RunLoop?1.2 RunLoop 和线程1.3 ...

  • RunLoop

    一、RunLoop是什么二、RunLoop的内部结构 1、RunLoop和线程的关系 2、RunLoop和Mode...

  • 『RunLoop』

    RunLoop 文章目录 RunLoop简介 1.1 什么是RunLoop? 1.2 RunLoop和线程 1.3...

网友评论

      本文标题:SDWebimage 和Runloop

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