iOS 2019年5月学习记录

2019-06-14  本文已影响0人  quantiza

美团Graver框架

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

Method Swizzling

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

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

Crash

1、crash原因

1) 野指针

Notifications

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

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

  1. Functors, Applicatives, And Monads

reference: Functors, Applicatives, And Monads In Pictures

image.png
  1. Reactive Programming
  1. FRP


    image.png

cold/hot signal

memgraph

移动平台

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

load & initialize

image.png

启动优化

pre-main阶段优化

post-main阶段优化

上一篇下一篇

猜你喜欢

热点阅读