• iOS 小经验:UIAnimation空对象导致crash


    今天调试一个老程序,发现有时运行动画会crash,仔细检查了基本的内存管理,发现都没问题!后来发现了问题,这个程序用的是下面的代码

    [self performSelectorInBackground:@selector(noticeDrawer) withObject:nil];

    其中,noticeDrawer函数如下,

    - (void)noticeDrawer

    {

        [[NSNotificationCenter defaultCenter] postNotificationName:kBackToMenu object:nil userInfo:nil];

    }

    这里noticeDrawer函数,发出了一个notification,这个notification的相关函数是一个动画函数。那么在一个子线程里post 一个notification,这个notification的响应函数会在哪个线程里调用呢?

    - (void)addObserver:(id)notificationObserver
               selector:(SEL)notificationSelector
                   name:(NSString *)notificationName
                 object:(id)notificationSender

    函数里,没有找到关于运行线程的描述,但是在另一个方法中:

    - (id<NSObject>)addObserverForName:(NSString *)name
                                object:(id)obj
                                 queue:(NSOperationQueue *)queue
                            usingBlock:(void (^)(NSNotification *note))block

    有如下的描述:

    queue

    The operation queue to which block should be added.

    If you pass nil, the block is run synchronously on the posting thread.

    由次猜测,使用

    - (void)addObserver:(id)notificationObserver
               selector:(SEL)notificationSelector
                   name:(NSString *)notificationName
                 object:(id)notificationSender
    方法是会在发出notification的线程里进行函数调用的,
    这就解释了为什么开始的程序有问题了,它在子线程里操作UI,这个当然会引起各种问题了。
    解决方法就是去掉
    performSelectorInBackground这个函数(当时应该就是写错了才用了这个函数吧)。
  • 相关阅读:
    windows下安装nginx
    java 32位MD5加密的大写字符串
    eclipse运行maven的jetty插件内存溢出
    Phpstorm Alt+Enter 自动导入类
    CSS 再学习,文本处理
    1406 data too long for column 'content' at row 1
    tp5.1报错 页面错误!请稍后再试
    lnmp升级php
    Tp5,Service使用
    CSS再学习 之背景色 背景图片
  • 原文地址:https://www.cnblogs.com/breezemist/p/4784649.html
Copyright © 2020-2023  润新知