性能优化之 NSDateFormatter

作者:tingxins
链接:https://www.jianshu.com/p/82c1104aea6c#nsdateformatter
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

为什么要优化NSDateFormatter?
首先,过度的创建NSDateFormatter用于NSDate与NSString之间转换,会导致App卡顿,打开Profile工具查一下性能,你会发现这种操作占CPU比例是非常高的。据官方说法,创建NSDateFormatter代价是比较高的,如果你使用的非常频繁,那么建议你缓存起来,缓存NSDateFormatter一定能提高效率。
优化方式有哪些?

a.延迟转换
即只有在UI需要使用转换结果时在进行转换。

b.Cache in Memory
根据NSDateFormatter线程安全性,不同的iOS系统版本内存缓存如下:

prior to iOS 7
如果直接采用静态变量进行存储,那么可能就会存在线程安全问题,在iOS 7之前,NSDateFormatter是非线程安全的,因此可能就会有两条或以上的线程同时访问同一个日期格式化对象,从而导致App崩溃。

+ (NSDateFormatter *)cachedDateFormatter {

NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary];

NSDateFormatter *dateFormatter = [threadDictionary objectForKey:@"cachedDateFormatter"];

if (!dateFormatter) {

dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setLocale:[NSLocale currentLocale]];

[dateFormatter setDateFormat: @"YYYY-MM-dd HH:mm:ss"];

[threadDictionary setObject:dateFormatter forKey:@"cachedDateFormatter"];

}

return dateFormatter;

}

iOS 7 or later
在iOS 7、macOS 10.9及以上系统版本,NSDateFormatter都是线程安全的,因此我们无需担心日期格式化对象在使用过程中被另外一条线程给修改,为了提高性能,我们还可以在上述代码块中进行简化(除去冗余部分)。

static NSDateFormatter *cachedDateFormatter = nil;

+ (NSDateFormatter *)cachedDateFormatter {

// If the date formatters aren't already set up, create them and cache them for reuse.

if (!dateFormatter) {

dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setLocale:[NSLocale currentLocale]];

[dateFormatter setDateFormat: @"YYYY-MM-dd HH:mm:ss"];

}

return dateFormatter;

}

如果缓存了日期格式化或者是其他依赖于current locale的对象,那么我们应该监听NSCurrentLocaleDidChangeNotification通知,当current locale变化时及时更新被缓存的日期格式化对象。

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

推荐阅读更多精彩内容

  • 为什么要优化NSDateFormatter? 首先,过度的创建NSDateFormatter用于NSDate与NS...
    Crazy2015阅读 715评论 0 0
  • 为什么要优化NSDateFormatter? 优化方式有哪些? 为什么要优化NSDateFormatter? 首先...
    tingxins阅读 7,211评论 2 20
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,251评论 4 61
  • 不知道在什么时候 也不知道在哪一天 你悄悄拨动了我的心弦 奏出了一曲美秒的乐章 有一种感觉叫妙不可言 如梦似幻,幸...
    yzwjjx阅读 117评论 0 0
  • “你我都不可能摆脱时钟的束缚,彼此都已沦为社会这个时钟的齿轮。一旦少了齿轮,时钟就会出乱子。纵然自己渴望率性而为,...
    SEVEN_24阅读 1,270评论 0 2