Notification Once

本文介绍了一种优雅地在iOS应用启动时初始化模块的方法,通过使用Notification而非直接在AppDelegate中进行,使得代码更加整洁并遵循单一职责原则。

这个优化非常不错,,,收藏了。。。

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

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // ...
    [FooModule setup];
    [[BarModule sharedInstance] setup];
    // ...
    return YES;
}

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

/// 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>的方法外不应该出现其他方法。


### Notification Workflow in Software Development In software development, notifications play an essential role in informing users or systems about specific events or changes within applications. The notification workflow typically involves several stages from event detection to delivery. #### Event Detection The process begins when certain predefined conditions are met inside the system. These could be user actions such as submitting a form, completing a task, or reaching milestones like deadlines being approached. In some cases, these triggers might also come from external sources through APIs or webhooks[^1]. #### Processing Logic Once triggered, processing logic determines what type of message should be sent based on rules defined either statically during development time or dynamically via configuration files. This stage may involve checking permissions, personalizing messages according to recipient preferences (e.g., language), and determining appropriate channels for sending out alerts—email, SMS, push notifications, etc.[^2] #### Queue Management To ensure reliable delivery even under high load scenarios where immediate response isn't always possible due to resource constraints, many implementations use queues. Messages get placed into this buffer temporarily until they can safely reach their destination without causing performance issues elsewhere within the application stack[^3]. ```python import queue def enqueue_notification(notification): q = queue.Queue() q.put(notification) def send_notifications(): while not q.empty(): notify = q.get() # Send each notification here... ``` #### Delivery Mechanism Finally, once processed and dequeued appropriately, actual transmission occurs over chosen mediums directly connecting back-end services with end-users' devices. For instance, emails would go through SMTP servers; mobile pushes require integration with platform-specific providers like Firebase Cloud Messaging (FCM).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值