ios - 我在一个线程里起了一个runloop,同时我在线程中,开启了一个timer,但我发现我的类无法释放,求大神们帮忙看看
怪我咯
怪我咯 2017-04-18 09:16:34
[iOS讨论组]

1.我自定义了一个类,叫RunloopThread
2.我在RunloopThread里创建了一个线程,然后在线程里起了一个runloop
3.往runloop里添加了一个timer

问题 : 我发现无法释放这个类

代码如下:

#import "RunloopThread.h"

@interface RunloopThread ()

@property (nonatomic, strong)NSTimer *archiveringTimer;

@end

static NSThread *thread;
static NSPort *port;

@implementation RunloopThread

-(void)dealloc
{

}

-(instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

-(void)start{
    [self _d_cleaningInBackgroudSchedule];
}

-(void)closeSelf{
    NSRunLoop *loop = [NSRunLoop currentRunLoop];
    [loop removePort:port forMode:NSDefaultRunLoopMode];
    [_archiveringTimer invalidate];
    _archiveringTimer = nil;
    [NSThread exit];
    thread = nil;
    CFRunLoopStop(CFRunLoopGetCurrent());
}

+(NSThread *)workThread {
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        if (!thread) {
            thread = [[NSThread alloc] initWithTarget:self
                                             selector:@selector(networkRequestThreadEntryPoint)
                                               object:nil];
            [thread start];
            
        }
        
    });
    return thread;
}

+(void)networkRequestThreadEntryPoint{
    @autoreleasepool {
        
        NSThread *thread = [NSThread currentThread];
        thread.name = @"com.thread.archive";
        
        NSRunLoop *runloop = [NSRunLoop currentRunLoop];
        port = [NSMachPort port];
        [runloop addPort:port forMode:NSDefaultRunLoopMode];
        
        CFRunLoopRun();
        
    }
}

-(void)_d_cleaningInBackgroudSchedule {
    if (!thread) {
        [self performSelector:@selector(_d_cleaningInBackgroud)
                     onThread:[[self class] workThread]
                   withObject:nil
                waitUntilDone:NO];
    }
    
}

-(void)_d_cleaningInBackgroud{
    //1. 获取当前线程的runloop
    NSRunLoop *loop = [NSRunLoop currentRunLoop];
    //2.创建timer
    _archiveringTimer = [NSTimer timerWithTimeInterval:1
                                                target:self
                                              selector:@selector(_d_cleaningInBackgroud2)
                                              userInfo:nil
                                               repeats:YES];
    //3. 将NSTimer添加到runloop
    [loop addTimer:_archiveringTimer forMode:NSDefaultRunLoopMode];
}

static int ii = 0;

-(void)_d_cleaningInBackgroud2{
    
    BOOL y = [NSThread isMainThread];
    
    if (!y) {
        NSLog(@"我在异步处理点事情");
        
        ii ++;
        
        if ( ii == 5) {
            [self closeSelf];
        }
    }
    
}

@end

请问各位大神,我的问题出在哪里,要怎么解决呢?

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(1)
PHP中文网

[NSThread exit]; 去掉就好了
可以看看这个深入理解RunLoop

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号