RunLoop在项目中的应用

源码:https://github.com/baitxaps/Block

//1.AFNetworkingRunLoop的创建

- (void)netWorkRequestThreadEntryPoint:(id)__unused  object{

    @autoreleasepool {

        [[NSThreadcurrentThread]setName:@"AFNetworking"];

        

        NSRunLoop *runLoop = [NSRunLoopcurrentRunLoop];

        [runLoop addPort:[NSMachPortport] forMode:NSDefaultRunLoopMode];

        

        [runLoop run];

    }

}


- (NSThread *)networkRequestThread{

    static NSThread *_networkRequestThread =nil;

    static dispatch_once_t oncePreadicate;

    dispatch_once(&oncePreadicate, ^{

        _networkRequestThread = [[NSThread alloc]initWithTarget:selfselector:@selector(netWorkRequestThreadEntryPoint:)object:nil];

        [_networkRequestThread start];

    });

    return _networkRequestThread;

}


//2.接到crashsingal后动重启RunLoop

- (void)restartRuningRunLoop{

    CFRunLoopRef runloop =CFRunLoopGetCurrent();

    NSArray *allModes =CFBridgingRelease(CFRunLoopCopyAllModes(runloop));

    while(1){

        for(NSString *modein allModes){

            CFRunLoopRunInMode((CFStringRef)mode,0.001,false);

        }

    }

}

//3.TableView 延迟加载图片新思路

#if TARGET_OS_IPHONE

UIImageView *imageView;

- (void)delayLoadingImage{

    

    UIImage *downLoadImage = nil;

    [imageView performSelector:@selector(setImage:)

                    withObject:downLoadImage

                    afterDelay:0

                       inModes:@[NSDefaultRunLoopMode]]

}

#elif TARGET_OS_MAC

//...

#endif


- (void)content:(NSString *(^)(NSString * content))block{

    

}


//4.异步测试

- (void)runUnitlBlock:(BOOL(^)())block timeout:(NSTimeInterval)timeout{

    NSDate *timeoutDate = [NSDatedateWithTimeIntervalSinceNow:timeout];

    do{

        CFTimeInterval quantum = 0.0001;

        CFRunLoopRunInMode(kCFRunLoopDefaultMode,quantum,false);

    }while ([timeoutDate timeIntervalSinceNow]>0 &&!block()) ;

}


//4.1升级版异步测试

- (BOOL)upgradeRunUnitlBlock:(BOOL(^)())block timeout:(NSTimeInterval)timeout{

    __block Boolean fulfilled =NO;

    void(^beforeWaiting)(CFRunLoopObserverRef observer,CFRunLoopActivity activity)=

    ^(CFRunLoopObserverRef observer,CFRunLoopActivity activity){

        fulfilled = block();

        if (fulfilled) {

            CFRunLoopStop(CFRunLoopGetCurrent());

        }

    };

    

    CFRunLoopObserverRef observer =CFRunLoopObserverCreateWithHandler(NULL,\

                                  kCFRunLoopBeforeWaiting,true, 0, beforeWaiting);

    

    CFRunLoopAddObserver(CFRunLoopGetCurrent(), observer,kCFRunLoopDefaultMode);

    

    //run

    CFRunLoopRunInMode(kCFRunLoopDefaultMode, timeout,false);

    CFRunLoopRemoveObserver(CFRunLoopGetCurrent(), observer,kCFRunLoopDefaultMode);

    CFRelease(observer);

    

    return fulfilled;

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值