zoukankan      html  css  js  c++  java
  • iOS_某操作霸占主线程过久导致“界面假死”的一种解决方法

    http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html#//apple_ref/doc/uid/10000057i-CH1-SW1

    /*首先往消息中心注册一个检测针对的observer。*/
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleResult:) name:@"PostNO." object:nil];
    
    /*然后创建一个线程做之前耗时过久的操作。*/
    [NSThread detachNewThreadSelecto:@selector(calculate) toTarget:self withObject:nil];
    
    /*耗时的操作*/
    - (void)caculate
    {
        /*新建的线程必须创建自己的内存释放池!*/
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSString *result = @"1111";
        /*事情做完后告知消息中心*/
        [[NSNotificationCenter defaultCenter] postNotificationName:"PostNO." object:result];
        [pool release];
    }
    /*耗时操作执行完后的回调函数*/
    - (void)handleResult:(NSNotification *)noti
    {
        id obj = [noti object];
        [self performSelectorOnMainThread:@selector(getResult:) withObject:obj waitUtilDone:YES];
    }
    /*在主线程进行的操作*/
    - (void)getResult:(id)result
    {
        //do some thing(更新UI界面之类的)
    }

     记得结束后,把observer从NSNotificationCenter 中remover掉

  • 相关阅读:
    02.创建型————工厂方法模式
    01.创建型————简单工厂模式
    HBase JavaAPI操作示例
    MongoDB
    大数据第三天
    Zookeeper操作
    MR操作
    HDFS操作
    【GISER&&Painter】svg的那些事
    读法克鸡丝博文《技术,产品,团队》有感
  • 原文地址:https://www.cnblogs.com/kelisi-king/p/3223727.html
Copyright © 2011-2022 走看看