验证通知是同步还是异步执行....据代码验证,通知其实是一个同步执行的过程,按照通知中心发布通知,然后订阅通知,接收通知step by step
2017-05-01 11:41
302 查看
//
// ViewController.m
//
演练通知是同步还是异步
//
// Created by doublek on 2017/5/1.
// Copyright © 2017年
doublek. All rights reserved.
//
#import
"ViewController.h"
@interface
ViewController ()
@end
//重用标识
static
NSString *kNotificationName =
@"NSNotificationName";
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
UIButton *button = [UIButton
buttonWithType:UIButtonTypeContactAdd];
button.center =
self.view.center;
[self.view
addSubview:button];
[button addTarget:self
action:@selector(buttonclick)
forControlEvents:UIControlEventTouchUpInside];
//注册通知
[[NSNotificationCenter
defaultCenter]
addObserver:self
selector:@selector(actionNotification:)
name:kNotificationName
object:nil];
}
-(void)actionNotification:(NSNotification
*)notification{
NSLog(@"发布通知");
//睡眠三秒
sleep(3);
NSString *message = notification.object;
NSLog(@"%@",message);
}
-(void)buttonClick{
//接收通知
[[NSNotificationCenter
defaultCenter]
postNotificationName:kNotificationName
object:@"通知已经被接收"];
NSLog(@"我被点击了");
}
// ViewController.m
//
演练通知是同步还是异步
//
// Created by doublek on 2017/5/1.
// Copyright © 2017年
doublek. All rights reserved.
//
#import
"ViewController.h"
@interface
ViewController ()
@end
//重用标识
static
NSString *kNotificationName =
@"NSNotificationName";
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
UIButton *button = [UIButton
buttonWithType:UIButtonTypeContactAdd];
button.center =
self.view.center;
[self.view
addSubview:button];
[button addTarget:self
action:@selector(buttonclick)
forControlEvents:UIControlEventTouchUpInside];
//注册通知
[[NSNotificationCenter
defaultCenter]
addObserver:self
selector:@selector(actionNotification:)
name:kNotificationName
object:nil];
}
-(void)actionNotification:(NSNotification
*)notification{
NSLog(@"发布通知");
//睡眠三秒
sleep(3);
NSString *message = notification.object;
NSLog(@"%@",message);
}
-(void)buttonClick{
//接收通知
[[NSNotificationCenter
defaultCenter]
postNotificationName:kNotificationName
object:@"通知已经被接收"];
NSLog(@"我被点击了");
}
相关文章推荐
- 【Step by Step】编写代码验证一个ASP.NET应用程序和页面的生命周期
- jquery ajax 同步异步的执行示例代码
- 为什么按照微软给定的匿名配置Web 同步最终造成创建订阅的步骤总是失败?但改为需要身份验证就行了
- jquery ajax 同步异步的执行示例代码
- 每日学习心得:CustomValidator验证控件验证用户输入的字符长度、Linq 多字段分组统计、ASP.NET后台弹出confirm对话框,然后点击确定,执行一段代码
- 分享一个分布式消息总线,基于.NET Socket Tcp的发布-订阅框架,附代码下载 推荐
- 下面画一个简单托管代码和非托管代码的执行过程:
- 利用wojilu框架仿一个网站的全过程(Step by Step利用wojilu框架开发网站系列一 附源码)
- 发布一个史上最简单代码最少Javascript Timer,解决一切定时执行的问题
- 每日学习心得:CustomValidator验证控件验证用户输入的字符长度、Linq 多字段分组统计、ASP.NET后台弹出confirm对话框,然后点击确定,执行一段代码
- 分享一个分布式消息总线,基于.NET Socket Tcp的发布-订阅框架,附代码下载
- ext异步请求一个aspx/ashx/web service取得数据,服务端返回一个json.然后客户端接收并显示.
- [置顶] 判断用户的请求类型 get or post 然后同步 或者 异步 执行 方法
- 当你输入信用卡号码的时候,有没有担心输错了而造成损失呢?其实可以不必这么担心,因为并不是一个随便的信用卡号码都是合法的,它必须通过Luhn算法来验证通过。 该校验的过程:1、从卡号最后一位数字开始,逆
- Step by Step 创建一个简单的Silverlight应用程序(转自微软学生中心)
- 因为应用代码正在发送一个输入同步呼叫,所以无法执行传出地呼叫
- 代码的同步和异步执行
- 利用wojilu框架仿一个网站的全过程(Step by Step利用wojilu框架开发网站系列---序言)
- js方法内Ajax请求数据判断,验证无效(OnClientClick="return Method();"),还是直接执行后台代码
- <<Windows核心编程(第五版)>>第十章同步设备I/O与异步设备I/O:10.5接收I/O请求完成通知