iOS 2019年5月学习记录

美团Graver框架

1、将多个视图层级渲染成一张bitmap

Method Swizzling

Swizzling should always be done in +load.
Swizzling should always be done in a dispatch_once.

每一个对象地址对应一个 ObjectAssociationMap 对象,而一个 ObjectAssociationMap 对象保存着这个对象的若干个关联记录。

  • 关联对象与被关联对象本身的存储并没有直接的关系,它是存储在单独的哈希表中的;
  • 关联对象的五种关联策略与属性的限定符非常类似,在绝大多数情况下,我们都会使用OBJC_ASSOCIATION_RETAIN_NONATOMIC 的关联策略,这可以保证我们持有关联对象;
  • 关联对象的释放时机与移除时机并不总是一致,比如实验中用关联策略 OBJC_ASSOCIATION_ASSIGN 进行关联的对象,很早就已经被释放了,但是并没有被移除,而再使用这个关联对象时就会造成 Crash 。
  • 无法很好的处理weak指针,通过OBJC_ASSOCIATION_ASSIGN,代表unsafe_unretain

Crash

1、crash原因

1) 野指针

  • 原因
    OC对象在释放之后内存空间可以被重新分配,在分配之前内存数据还是存在的,这就是僵尸对象。OC对象释放后,如果还存在指向该内存的指针没有设为nil,该指针就是野指针(wild pointer)。野指针还指指向未申请访问受限的内存空间
  • 随机性
    当Zombie Object所在的内存还没有被重新分配时,通过野指针访问是可以成功的;但内存已经被重新分配,再用野指针访问就会出错。
  • 检测
    Xcode可以开启Zombie Object检测,如果开启,在每次访问对象是都会检测该对象是否是僵尸对象。当一个对象被释放后确保指针设为nil。
  • 必现
    通过scheme=>diagnostics=>Enable Scribble设置,在释放的对象所在的内存填上不可访问的数据,这样会引起objc_msgSend()调用错误,或者各种SIG错误。
    也可以通过hook NSObject的dealloc,runtime obj_dispose, c的free来在对象释放时向所在内存写入数据。

Notifications

1 why notification
notification实现了一对多的通知机制,通知的接受者(observer)和发送者(sender)互不了解,从而实现解耦
2 线程安全
Notification的post线程与observer的接收回调在同一个线程中。如果要修改接收回调的线程有两种方案

  • 使用 NSPort转发。
  • 使用 block API指定block执行的queue

3 线程同步性
postNotification:总是会卡住当前线程,待observer执行(如不特殊处理selector也会在postNotification:所在线程执行)结束之后才会继续往下执行。
4 异步通知
将NSNotification放入NSNotificationQueue,然后根据其type,NSNotificationQueue在合适的时机将其post到NSNotificationCenter。这样就完成了异步的需求。还可以使用queue进行notification合并

typedef NS_ENUM(NSUInteger, NSPostingStyle) {
    NSPostWhenIdle = 1,      // 当runloop处于空闲状态时post
    NSPostASAP = 2,    // 当当前runloop完成之后立即post
    NSPostNow = 3    // 立即post,同步会阻塞post线程
};
typedef NS_OPTIONS(NSUInteger, NSNotificationCoalescing) {
    NSNotificationNoCoalescing = 0,  // 不合成
    NSNotificationCoalescingOnName = 1,  // 根据NSNotification的name字段进行合成
    NSNotificationCoalescingOnSender = 2  // 根据NSNotification的object字段进行合成
};
// 方式1:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    A *a = [A new];
    [a test];
    self.a = a;
    NSNotification *noti = [NSNotification notificationWithName:@"111" object:nil];
    [[NSNotificationQueue defaultQueue] enqueueNotification:noti postingStyle:NSPostASAP coalesceMask:NSNotificationNoCoalescing forModes:nil];
    [[NSNotificationQueue defaultQueue] enqueueNotification:noti postingStyle:NSPostWhenIdle coalesceMask:NSNotificationNoCoalescing forModes:nil];
    [[NSNotificationQueue defaultQueue] enqueueNotification:noti postingStyle:NSPostNow coalesceMask:NSNotificationNoCoalescing forModes:nil];
    NSLog(@"测试同步还是异步");
    return YES;
}
// 方式2:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    A *a = [A new];
    [a test];
    self.a = a;
    NSNotification *noti = [NSNotification notificationWithName:@"111" object:nil];
    [[NSNotificationQueue defaultQueue] enqueueNotification:noti postingStyle:NSPostASAP coalesceMask:NSNotificationCoalescingOnName forModes:nil];
    [[NSNotificationQueue defaultQueue] enqueueNotification:noti postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
    [[NSNotificationQueue defaultQueue] enqueueNotification:noti postingStyle:NSPostNow coalesceMask:NSNotificationCoalescingOnName forModes:nil];
    NSLog(@"测试同步还是异步");
    return YES;
}

result:
// 方式一
2017-02-26 20:09:31.834 notification[20612:12733161] selector 1
2017-02-26 20:09:31.835 notification[20612:12733161] block 2
2017-02-26 20:09:31.835 notification[20612:12733161] 测试同步还是异步
2017-02-26 20:09:31.851 notification[20612:12733161] selector 3
2017-02-26 20:09:31.851 notification[20612:12733161] block 4
2017-02-26 20:09:31.854 notification[20612:12733161] selector 5
2017-02-26 20:09:31.855 notification[20612:12733161] block 6
// 方式二
2017-02-26 20:11:31.186 notification[20834:12736113] selector 1
2017-02-26 20:11:31.186 notification[20834:12736113] block 2
2017-02-26 20:11:31.186 notification[20834:12736113] 测试同步还是异步

5 dealloc remove
iOS8及以前,NSNotificationCenter持有的是观察者的unsafe_unretained指针(可能是为了兼容老版本),这样,在观察者回收的时候未removeOberser,而后再进行post操作,则会向一段被回收的区域发送消息,所以出现野指针crash。而iOS9以后,unsafe_unretained改成了weak指针,即使dealloc的时候未removeOberser,再进行post操作,则会向nil发送消息,所以没有任何问题。

RAC

FRP

  1. FR
    Functional programming is a programming paradigm — a style of building the structure and elements of computer programs — that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data — Wikipedia
    简单来讲FR是一种用数学函数的方法实现无side effect的编程范式。

  2. Concept

  • Pure functions:同样的输出同样的输出,不产生side effect
    文件操作通常不是pure的,因为文件可以被改变
    pure很容易通过测试
  • Immutability:不改变值或状态
    循环里会有值或状态的改变,可以用递归函数避免
    链式调用可以避免出现中间可变状态,一个输入经过多次处理产生一个输出
  • Referential transparency
    pure functions + immutable data = referential transparency
    一个函数的输入不变时,函数的输出也不变,所以对于同一个input可以直接用output来替代函数调用,这样可以优化程序性能,这种技术叫Memoization
  • Functions as first-class entities
    可以被变量引用
    可以作为参数传递
    可以作为函数输出
  • Higher-order functions (以函数作为参数或返回一个函数)
    在处理序列时通常通过循环来一个个处理序列中的元素(Imperative approach),使用高阶函数可以像自然语言表达一样处理序列(Declarative approach)。这种方式隐藏了实现过程,只关注具体事务。
  • reference: Functional Programming Principles in Javascript
  1. Functors, Applicatives, And Monads

reference: Functors, Applicatives, And Monads In Pictures

image.png
  1. Reactive Programming
  • 响应式编程是一种关注于数据流和数据传播变化的声明式编程范式(Reactive programming - wiki),这种范式可以优雅的表达静态(eg array)和动态( event emitters)数据流。
  • declarative programming只关注于逻辑表达,隐藏了实现过程。imperative programming会把所有的实现指令表达出来。declarative方便于面向对象,响应式开发等开发;imperative方便于算法开发。
  1. FRP


    image.png

cold/hot signal

  • Hot Observable是主动的,尽管你并没有订阅事件,但是它会时刻推送,就像鼠标移动;而Cold Observable是被动的,只有当你订阅的时候,它才会发布消息。
  • Hot Observable可以有多个订阅者,是一对多,集合可以与订阅者共享信息;而Cold Observable只能一对一,当有不同的订阅者,消息是重新完整发送。
  • RACSubject及其子类是热信号, RACSignal排除RACSubject类以外的是冷信号。

memgraph

移动平台

1、小程序解决了H5容器以下缺点

  • 安全:不允许直接操作dom
  • 性能:H5最终由webview渲染,小程序可以native也可以其他方式渲染
  • 开发效率

load & initialize

image.png

启动优化

pre-main阶段优化

  • 删除无用代码
  • 抽象重复代码
  • +load方法中做的事情延迟到+initialize中,或者在+load中做的事情不宜花费过多时间
  • 减少不必要的framework,或者优化已有的framework

post-main阶段优化

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,906评论 0 38
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,144评论 1 32
  • 1.设计模式是什么? 你知道哪些设计模式,并简要叙述?设计模式是一种编码经验,就是用比较成熟的逻辑去处理某一种类型...
    龍飝阅读 2,194评论 0 12
  • 只要智商低,天天过六一!
    阿珍你阅读 187评论 1 0
  • 看《穷查理宝典》中,认识了一个“社会认同倾向”心理模式。 意思是,人们会自动根据他看到的周边人们的思考和行动方式去...
    心诚事享cao阅读 250评论 0 3