iOS AppDelegate瘦身

个人建议

AppDelegate该文件中只处理app的delegate的事件,不进行其他代码的编写,已达到简洁明晰的作用

瘦身原理

利用+load的特性并结合notification来实现加载其他模块等在app启动时所需要完成的工作

代码(这段代码在AppDelegate里面写)
+ (void)load{
__block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
    
    //初始化appdelegate的帮助对象
    AppDelegateHelper *appDelegateHelper = [AppDelegateHelper new];
    [appDelegateHelper appSetUp];
    [[NSNotificationCenter defaultCenter] removeObserver:observer];
}];
}
代码说明
  1. +load会在足够早的时间进行调用
  1. block会生成一个__nsobserver *给外部remove观察者
  2. block对observer的捕捉早于函数的返回,如果不加__block,会返回nil
  3. 在block结束时,只需要清除observer,不需要做其他的清除工作
  4. 这样在app启动时,所有工作都可以完成,同是也可以保持appdelegate文件的简洁清晰

相关推荐

  1. iOS AppDelegate

    2024-05-11 06:50:08       34 阅读
  2. C盘,C盘清理

    2024-05-11 06:50:08       59 阅读
  3. Spring Boot应用(thinJar)

    2024-05-11 06:50:08       53 阅读
  4. 安卓之APK与资源压缩方案

    2024-05-11 06:50:08       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-11 06:50:08       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-11 06:50:08       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-11 06:50:08       82 阅读
  4. Python语言-面向对象

    2024-05-11 06:50:08       91 阅读

热门阅读

  1. 常用的启发式算法:探索问题解决的智慧之道

    2024-05-11 06:50:08       33 阅读
  2. XMLHttpRequest与Axios详解

    2024-05-11 06:50:08       26 阅读
  3. React 之 forwardRef用法(十六)

    2024-05-11 06:50:08       28 阅读
  4. DNS-域名系统

    2024-05-11 06:50:08       25 阅读
  5. SearXNG - 一个注重隐私的互联网元搜索引擎

    2024-05-11 06:50:08       26 阅读
  6. ubuntu中运行c# winform程序

    2024-05-11 06:50:08       31 阅读
  7. TensorFlow与PyTorch:哪个更适合深度学习项目?

    2024-05-11 06:50:08       31 阅读
  8. CentOS常见命令

    2024-05-11 06:50:08       29 阅读
  9. AI智能体|智谱清言开放了智能体API

    2024-05-11 06:50:08       30 阅读
  10. 【第66例】IPD体系进阶:华为IPD发展历程

    2024-05-11 06:50:08       33 阅读
  11. 百度智能云数据仓库 Palo 实战课程

    2024-05-11 06:50:08       33 阅读