zoukankan      html  css  js  c++  java
  • 自定义NSOperation

    一直在思考,每次异步请求都会创建一个新线程,如果我同时发100个异步请求,这样会导致我的内存爆满,应用程序奔溃,因为iOS对开线程有着约束,不能开很多个线程,这就要求我们必须对异步请求进行控制,我一直都试图将异步请求放在NSOperationque多线程中,让队列去进行管理,可是这样会造成开更多的线程,并且不能直接对异步请求进行诸如[queue addOperation:operation]语法的实现。因为从SDWebImage的原理看出,它就是用了多线程并且使用了NSURLConnection进行了进度监听,自定义NSOperation,只要加入这样一行代码,就能实现,解法1:NSOperationQueue *queue=[[NSOperationQueue alloc]init];[conn setDelegateQueue:queue]。

    解法2: //循环的目的:runloop一直运行, 造成当前线程阻塞, 保持线程不结束,自定义子线程: runloop默认不开启

        while ( !isFinished ) {

            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

        }

    再通过

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            {

                NSLog(@"%@",[NSThread currentThread]);

            }

        }];   

  • 相关阅读:
    Codeforces 868A Bark to Unlock
    poj 3744 Scout (Another) YYF I
    Codeforces 526F Pudding Monsters
    Codeforces 789D Weird journey
    Codeforces 827E Rusty String
    Codeforces 841D Leha and another game about graph
    bzoj 3560 DZY Loves Math V
    Codeforces 833A The Meaningless Game
    Codeforces 839D Winter is here
    Codeforces 839C Journey
  • 原文地址:https://www.cnblogs.com/xsiOS/p/5081314.html
Copyright © 2011-2022 走看看