美文网首页iOS开发
NotificationCenter与线程相关

NotificationCenter与线程相关

作者: Dywane | 来源:发表于2018-02-11 16:23 被阅读12次

NotificationCenter是基于iOS程序内部之间的一种消息广播机制,主要是为了解决应用程序不同对象之间通信解藕而设计。它基于KVO模式设计,当收到通知后由通知中心根据转发表将消息发给观察者。

在Apple的文档里面有如下说明:

A notification center delivers notifications to observers synchronously. In other words, when posting a notification, control does not return to the poster until all observers have received and processed the notification. To send notifications asynchronously use a notification queue, which is described in Notification Queues.

In a multithreaded application, notifications are always delivered in the thread in which the notification was posted, which may not be the same thread in which an observer registered itself.

其实它主要是指NotificationCenter是一种“同步的”消息机制,当一个通知被post之后,他将会在所有的观察者都完成了对应的方法后才会返回。在多线程的应用中,通知是在哪个线程Post就在哪个线程分发,也就是在同一个线程被观察者处理,所以观察者的回调函数是完全由发送消息的线程决定,而不是由注册时所在的线程决定。

关于这一点我举一下我在对聊天消息进行优化的时候发现的问题作为例子,在原来的代码结构中,需要处理获取的消息,并更新会话cell的排序:

swift

ConversationModel.swift

func handleMessage() {
    var message = Message()
    /* do something to create and process messages */
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: kRefreshConversation), object: self)
}

ConversationVC.swift

func viewDidLoad() {
    NotificationCenter.default.addObserver(self, selector: #selector(refreshConversatin(_:)), name: NSNotification.Name(rawValue: kRefreshConversation), object: nil)
    /* other things ...*/
}

@objc private func refreshConversatin(_ noti: Notification) {
    /* use a long time to sort conversation */
    tableview.reloadData() 
}

在原来这种处理方式中,由于refreshConversation这个方法会耗费大量时间,而这个通知是在主线程发起的,所以会导致主线程被阻塞,所以我针对这个问题对ConversationModel进行了重构。

首先我将Conversation获取并组装Message的这一块内容都放置在后台线程进行,因为Model相关的操作可能会占用很多的时间,放在主线程创建并组装是不可靠的。当message组装完毕需要更新ConversationVC中的排序时,在ConversationVC中接收到通知后再转到主线程进行操作。同时我在排序的代码中也进行了优化,如果ConversationVC没有被展示的话,就不需要占用主线程调用tableViewtableView.insertRows方法,仅仅需要在后台线程中对ConversationModel的数组进行排序即可。因为tableView.insertRows会占用大量主线程时间,所以减少使用该方法的频率,只在必要时候调用,将排序等操作放到后台线程进行是比较好的解决方法。


想了解更多内容可以查看我的博客

相关文章

  • NotificationCenter与线程相关

    NotificationCenter是基于iOS程序内部之间的一种消息广播机制,主要是为了解决应用程序不同对象之间...

  • iOS面试进阶篇(二)

    目录 UITableViewCell相关试题多线程相关试题进程与线程相关试题网络相关试题TCP与UDPTCP连接的...

  • Android下多线程的实现

    Android下多线程相关 线程安全相关问题参考:java内存模型与线程 android下与多线程有关的主要有以下...

  • Mac 中对系统Sleep 与 Wake 的检测

    途径:notificationCenter 通过 NSWorkspace的notificationCenter中对...

  • 22.iOS底层学习之多线程原理

    本篇提纲:1、线程与进程2、多线程3、多线程相关面试题4、线程安全问题5、线程与runloop的关系 线程与进程 ...

  • 进程与线程、线程池

    进程与线程的相关总结进程与线程的简单解释进程: 基本的资源分配资源线程: 最小调度单元 线程安全 线程安全是多线程...

  • 反射、注解与依赖注入总结

    上一篇【线程、多线程与线程池总结】中主要记录线程、多线程相关概念,侧重于线程的Future使用与线程池的操作;同样...

  • Android中的多线程-(叫android只是好听 其实是ja

    与线程相关的方法 简单介绍一下与线程相关的方法,概念虽然有些无聊,但是对于多线程的应用上会有更好理解。 Objec...

  • Python多线程与多进程

    内容简述: 线程与进程的相关概念1、程序,进程,线程,多进程,多线程2、线程的生命周期3、并行与并发,同步与异步4...

  • 网络相关

    引文: 多线程相关 OC 语言相关 内存管理相关 UI视图相关 RunLoop相关 HTTP协议 HTTPS与网络...

网友评论

    本文标题:NotificationCenter与线程相关

    本文链接:https://www.haomeiwen.com/subject/jptltftx.html