zoukankan      html  css  js  c++  java
  • [OC]Singleton的一种简便实现方式

    static dispatch_queue_t xml_request_operation_processing_queue() {
        static dispatch_queue_t af_xml_request_operation_processing_queue;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            af_xml_request_operation_processing_queue = dispatch_queue_create("com.alamofire.networking.xml-request.processing", DISPATCH_QUEUE_CONCURRENT);
        });
    
        return af_xml_request_operation_processing_queue;
    }

    通过dispatch_once执行一次来保证af_xml_request_operation_processing_queue只有一次实例化的机会~

    这个方法能够很好的在任何类中调起单例的时候使用~

    下面为singleton启一个单独的线程

    + (void) __attribute__((noreturn)) networkRequestThreadEntryPoint:(id)__unused object {
        do {
            @autoreleasepool {
                [[NSThread currentThread] setName:@"AFNetworking"];
                [[NSRunLoop currentRunLoop] run];
            }
        } while (YES);
    }
    
    + (NSThread *)networkRequestThread {
        static NSThread *_networkRequestThread = nil;
        static dispatch_once_t oncePredicate;
        dispatch_once(&oncePredicate, ^{
            _networkRequestThread = [[NSThread alloc] initWithTarget:self selector:@selector(networkRequestThreadEntryPoint:) object:nil];
            [_networkRequestThread start];
        });
        
        return _networkRequestThread;
    }
     
     
    作者:W.M.steve
    出处:http://www.cnblogs.com/weisteve/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    URL vs. HTML 录制模式
    多机联合产生负载
    浏览器打开URL的方式和加载过程
    与前端性能相关的头信息
    HTTP协议结构
    前端优化
    前端性能测试工具
    Apache 服务器
    java回调机制
    不允许用大于号小于号,比较任意两个数字大小
  • 原文地址:https://www.cnblogs.com/weisteve/p/3052790.html
Copyright © 2011-2022 走看看