Notification Once

Notification Once

前段时间整理项目中的AppDelegate,发现很多写在- application:didFinishLaunchingWithOptions:中的代码都只是为了在程序启动时获得一次调用机会,多为某些模块的初始化工作,如:

1
2
3
4
5
6
7
8
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // ...
    [FooModule setup];
    [[BarModule sharedInstance] setup];
    // ...
    return YES;
}

其实这些代码完全可以利用Notification的方式在自己的模块内部搞定,分享一个巧妙的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
/// FooModule.m
+ (void)load
{
    __block id observer =
    [[NSNotificationCenter defaultCenter]
     addObserverForName:UIApplicationDidFinishLaunchingNotification
     object:nil
     queue:nil
     usingBlock:^(NSNotification *note) {
         [self setup]; // Do whatever you want
         [[NSNotificationCenter defaultCenter] removeObserver:observer];
     }];
}

解释:

  • + load方法在足够早的时间点被调用
  • block版本的通知注册会产生一个__NSObserver *对象用来给外部remove观察者
  • block对observer对象的捕获早于函数的返回,所以若不加__block,会捕获到nil
  • 在block执行结束时移除observer,无需其他清理工作
  • 这样,在模块内部就完成了在程序启动点代码的挂载

值得注意的是,通知是在- application:didFinishLaunchingWithOptions:调用完成后才发送的。
顺便提下给AppDelegate瘦身的建议:AppDelegate作为程序级状态变化的delegate,应该只做路由分发的作用,具体逻辑实现代码还是应该在分别的模块中,这个文件应该保持整洁,除了<UIApplicationDelegate>的方法外不应该出现其他方法。

时间: 2024-08-08 08:41:25

Notification Once的相关文章

通知栏Notification在不同手机上显示的问题总结

可以参照http://blog.csdn.net/vipzjyno1/article/details/25248021,这里面关于通知的写的不错,也很全面,我的这篇主要是记录自己在适配上遇到的问题. 通知的统一的创建方式: NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext); 而通知的管理则是使用NotificationManager是用来管理通知的,使用如下:先初始化用到的系统服务,然后调

Apple Notification Center Service--ANCS【转】

Apple Notification Center Service 转自:http://studentdeng.github.io/blog/2014/03/22/ancs/ MAR 22ND, 2014 | COMMENTS 名词解释与约定 名词解释 Apple Notification Center Service 简称 ANCS. ANCS 服务(iOS设备,如iPhone,iPad等)的publisher 称为 Notification Provider. 任意的ANCS服务的clien

Notification Centers 通知中心

Notification Centers 通知中心 A notification center manages the sending and receiving of notifications. It notifies all observers of notifications meeting specific criteria. The notification information is encapsulated in NSNotification objects. Client o

Mobile Push Notification

In one embodiment, a method includes sending to a mobile client computing device a first notification through a real-time push service, the first notification including content and being associated with a stateful object; the method also includes, in

Java 线程第三版 第四章 Thread Notification 读书笔记

一.等待与通知 public final void wait() throws InterruptedException 等待条件的发生. public final void wait(long timeout) throws InterruptedException 等待条件的发生.如果通知没有在timeout指定的时间内发生,它还是会返回. public final void wait(long timeout, int nanos) throws InterruptedException

Android-自定义Notification

Android-自定义Notification 2014年4月26日 消息栏的消息,想必各位Android发烧友很清楚知道是什么,比如我们下载了一个应用,它可能会定时推送些消息到我们的手机中,比如微信消息送达的时候,可能会在通知栏显示.本博文介绍如何自定义一个Notification,很简单的东西,这里小巫只是把它整理出来,奉上demo. 先来看看效果图: 附上源码:http://download.csdn.net/detail/wwj_748/7259815 有兴趣的朋友可以加本人创建的群,里

通知(Notification)

1.通知的基本用法 //创建 NotificationManager 实例 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new NotificationCompat.Builder(this) .setContentTitle("This is title") //标题 .setContent

Notification的基本用法以及使用RemoteView实现自定义布局

Notification的作用 Notification是一种全局效果的通知,在系统的通知栏中显示.既然作为通知,其基本作用有: 显示接收到短消息.即时信息等 显示客户端的推送(广告.优惠.新闻等) 显示正在进行的事物(后台运行的程序,如音乐播放进度.下载进度) Notification的基本操作: Notification的基本操作主要有创建.更新和取消三种.一个Notification的必要属性有三项,如果不设置的话在运行时会抛出异常: 小图标,通过setSmallIcon方法设置 标题,通

Android中使用Notification实现宽通知栏(Notification示例二)

Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer)中查看.通知栏和通知抽屉 (notificationdrawer)都是系统层面控制的,你可以随时查看,不限制于app. Notification 的设计: 作为android UI中很重要的组成部分,notification拥有专属于自己的设计准则. Notification的界面元素在通知抽屉中的

安卓学习之通知(Notification)

安卓中创建通知需要借助NotificationManager来对通知进行管理.可以调用Context的getsSystemService()方法来获得. getsSystemService()方法接收一个参数,这个参数是字符串,用于指定哪一个服务.Context.NOTIFICATION_SERVICE 就是指定通知服务. 这个方法返回一个Object对象,所欲需要进行强制转换. NotificationManager manager = (NotificationManager) getSys