• 成对出现
  • 意思很简单,NSNotificationCenter消息的接受线程是基于发送消息的线程的。也就是同步的,因此,有时候,你发送的消息可能不在主线程,而大家都知道操作UI必须在主线程,不然会出现不响应的情况。所以,在你收到消息通知的时候,注意选择你要执行的线程。下面看个示例代码

    //接受消息通知的回调
    - (void)test
    {
    if ([[NSThread currentThread] isMainThread]) {
    NSLog(@"main");
    } else {
    NSLog(@"not main");
    }
    dispatch_async(dispatch_get_main_queue(), ^{
    //do your UI
    }); } //发送消息的线程
    - (void)sendNotification
    {
    dispatch_queue_t defaultQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(defaultQueue, ^{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil];
    });
    }
    文/JamesYu(简书作者)
    原文链接:http://www.jianshu.com/p/a4d519e4e0d5
    著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

NSNotificationCenter 注意的更多相关文章

  1. iOS NSNotificationCenter详解

    通知中心的特点: 1:同步执行 2: 一对多发送消息 3: 降低程序耦合度 通知中心是单例,目的就是从任意一个发送消息到任意一个接收者,是同步执行的. 那么什么是同步呢? 用网上经典的说法,就是我叫朋 ...

  2. iOS之 利用通知(NSNotificationCenter)获取键盘的高度,以及显示和隐藏键盘时修改界面的注意事项

    我们在开发中会遇到这样的情况:调用键盘时需要界面有一个调整,避免键盘遮掩输入框. 但实现时你会发现,在不同的手机上键盘的高度是不同的.这里列举一下: //获取键盘的高度 /* iphone 6: 中文 ...

  3. IOS 消息机制(NSNotificationCenter)

    消息机制 NSNotificationCenter 一直都在频繁使用,但是却对其原理不是十分了解.今天就花些时间,把消息机制原理重头到尾好好过一遍. iOS 提供了一种 "同步的" ...

  4. IOS学习之NSNotificationCenter消息机制

    NSNotificationCenter是 Cococa消息中心,统一管理单进程内不同线程的消息通迅. 添加观察者接收通知: //添加通知中心观察者 [[NSNotificationCenter de ...

  5. 通知中心 NSNotificationCenter 的简单使用方法

    NSNotificationCenter(通知中心)   [注意]需再dealloc中移除观察者   获取通知中心单例对象 NSNotificationCenter *center=[NSNotifi ...

  6. Swift利用协议优化NSNotificationCenter

    NSNotificationCenter存在的问题 通知没有统一的命名格式 对于通知的命名没有强制的要求,一个项目里可能有多种不同的命名规则.比如: 1 2 3 4 5 6 class Barista ...

  7. 通知 - NSNotificationCenter

    1.每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信: 2.任何一个对象都可以向通知中心发布通知(NSNotification), 描 ...

  8. NSNotificationCenter应用总结

    通常我们在 iOS 中发生什么事件时该做什么是由 Delegate 实现的,例如 View 加载完后会触发 viewDidLoad.  Apple 还为我们提供了另一种通知响应方式,那就是 NSNot ...

  9. 你真的了解NSNotificationCenter吗?

    一:首先查看一下关于NSNotificationCenter的定义 @interface NSNotificationCenter : NSObject { @package void * __str ...

  10. iOS中通知中心(NSNotificationCenter)的使用总结

    一.了解几个相关的类 1.NSNotification 这个类可以理解为一个消息对象,其中有三个成员变量. 这个成员变量是这个消息对象的唯一标识,用于辨别消息对象. @property (readon ...

随机推荐

  1. OpenACC 云水参数化方案

    ▶ 书上第十三章,用一系列步骤优化一个云水参数化方案.用于熟悉 Fortran 以及 OpenACC 在旗下的表现 ● 代码,文件较多,放在一起了 ! main.f90 PROGRAM main US ...

  2. win10下装的ubuntu14.04双系统,ubuntu系统访问win10磁盘问题

    参考:https://blog.csdn.net/u010426270/article/details/52420231 ubuntu下 解决方法: 1. 在终端输入如下命令,查看分区挂载情况 sud ...

  3. leetcode167

    public class Solution { public int[] TwoSum(int[] numbers, int target) { Dictionary<int, int> ...

  4. ERROR 1222 (21000): The used SELECT statements have a different number of columns :

    转自:https://blog.csdn.net/linshichen/article/details/52484224

  5. ABAP-关于隐式与显式的DB Commit

    转载:https://www.cnblogs.com/liaojunbo/archive/2011/07/11/2103491.html 1.显式的DB Commit 显式的DB Commit并没有对 ...

  6. 使用java代码执行linux命令

    前提: java代码是在windows下面写的,要打包放到linux下面运行,并且执行某个脚本. java代码: try { // 起作用的代码其实就下面这一行, 参数是linux中要执行的代码 Ru ...

  7. vue深入了解组件——动态组件&异步组件

    一.在动态组件上使用 keep-alive 我们之前曾经在一个多标签的界面中使用 is 特性来切换不同的组件: <component v-bind:is="currentTabComp ...

  8. WP8.1 发送邮件

    Method 1: Windows.System.Launcher.LaunchUriAsync(new Uri("abc@outlook.com?subject=hello world&a ...

  9. IExpress 制作安装包 注意事项

    被打包的文件名不能超过8个字符,否则iexpress会取前6个字符 + "~1".

  10. iOS Hardware Guide

    来自U3D文档 Hardware models The following list summarizes iOS hardware available in devices of various g ...