在非tracking的时候在主线程回调block,解决卡顿

在项目中,有时手势滑动的时候,如果执行了其它操作,会有卡顿感的感觉,如果能在手势滑动时,不作处理,等到滑动完成后,在回调,卡顿感会好些,下面上代码。


//
//  IDCAsyncRunloop.h
//
//  Copyright © 2018年 idol_ios. All rights reserved.
//


#import <Foundation/Foundation.h>


@interface IDCAsyncRunloop : NSObject
+ (void)registerInMainRunloopObserver;
+ (void)addTransactionContainer:(void (^)())block;

@end




//
//  IDCAsyncRunloop.m
//  Copyright © 2018年 idol_ios. All rights reserved.
//


#import "IDCAsyncRunloop.h"
 
@implementation IDCAsyncRunloop{
    
}


static NSHashTable* sContainerFuns = nil;

+ (NSHashTable*)containerFuns{
    return sContainerFuns;
}


static void _transactionRunLoopObserverCallback(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info)
{
    @synchronized([IDCAsyncRunloop class]) {
        for (void (^block)() in [IDCAsyncRunloop containerFuns]) {
            if (block) {
                block();
            }
        }
        
        [[IDCAsyncRunloop containerFuns] removeAllObjects];
    }
}




+ (void)addTransactionContainer:(void (^)())block
{
    @synchronized(self) {
        if (sContainerFuns == nil) {
            sContainerFuns = [NSHashTable hashTableWithOptions:NSPointerFunctionsObjectPointerPersonality];
        }
        
        [sContainerFuns addObject:block];
    }
}






+(void)registerInMainRunloopObserver{
    
    static CFRunLoopObserverRef observer;
    
    NSAssert([NSThread isMainThread], @"[IDCAsyncRunloop registerInMainRunloopObserver] must be used in main thread.");
    
    // defer the commit of the transaction so we can add more during the current runloop iteration
    CFRunLoopRef runLoop = CFRunLoopGetCurrent();
    CFOptionFlags activities = (kCFRunLoopBeforeWaiting | // before the run loop starts sleeping
                                kCFRunLoopExit);          // before exiting a runloop run
    CFRunLoopObserverContext context = {
        0,           
        NULL,  
        NULL,
        NULL
    };
    
    observer = CFRunLoopObserverCreate(kCFAllocatorDefault,        // allocator
                                       activities,  // activities
                                       YES,         // repeats
                                       INT_MAX,     // order after CA transaction commits
                                       &_transactionRunLoopObserverCallback,  // callback
                                       &context);   // context
    CFRunLoopAddObserver(runLoop, observer, kCFRunLoopDefaultMode);
    
    
    
}

@end



------------------------

调用方法,

1 :在Appdelegate中的didFinishLaunchingWithOptions调用:

[IDCAsyncRunloop registerInMainRunloopObserver];


2:正式的调用示例,如在ASIHttpRequest框架中,请求数据

// 3.创建操作对象
AFHTTPRequestOperation *operation = [client HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
    [IDCAsyncRunloop addTransactionContainer:^{
        //在这里处理responseObject数据
    }];


.........



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值