NSDateFormatter使用思考
来源:互联网 发布:c语言有什么用? 编辑:程序博客网 时间:2024/06/05 20:14
标签(空格分隔): NSDateFormatter
最近看项目代码的时候,发现对NSDateFormatter的使用有一些不好的地方,由于NSDateFormatter的生成很消耗资源,因此不建议每次使用都重新生成一个,而是使用一个全局的NSDateFormatter来完成所有需求。项目中,发现cell绑定数据时每次都会重新生成一个NSDateFormatter对象,这样使用会有一定耗时,虽然新手机硬件性能很高,无法感知这点时间,但是通过实际测试,还是发现使用全局的与每次重新生成,性能差距在50倍左右,还是值得优化的。
因此就想扩展NSDateFormatter,使其能根据每次date format 的不同,返回相应的NSDateFormatter,并且缓存相应的实例,下次访问可以直接获取,节省创建的时间,这样只在第一次创建的时候有一定时间消耗。stackoverflow有几个专门针对这个问题的讨论:
1. How to minimize the costs for allocating and initializing an NSDateFormatter?
2. Is caching a NSDateformatter application-wide good idea?
一些人从实际的项目经验中介绍了该如何全局访问,由于项目中一开始使用的场景大概有八种,基本都是设置date formatter格式的。方案大概经过了如下一些过程。
1. 设置一个全局的NSDateFormatter单例对象,每次使用都设置相应的date format,这种方案设计最简单,但是每次使用都需要根据需求设置不同的时间格式。如果存在多线程访问,就会有同步问题,因此这个方案不可行。
2. 增加一个NSDateFormatter分类,针对以上场景分别设置不同时间格式单例对象,这样就避免了每次修改时间格式的问题。但是接口太多,使用起来繁琐。
3. 基于以上,决定提供一个统一接口,使用只要传进来一个时间格式,就返回一个对象。内部对于对象保存起来,下次请求直接返回已经保存的对象,这样节省好多时间。同时要考虑线程安全性,准备借鉴国外用户的做法,将对象保存到线程dictionary里面。
+ (NSDateFormatter *)dateFormatterWithFormatString:(NSString *)format{// 版本1.// static dispatch_once_t onceToken;// static NSMutableDictionary *formatterDic;// dispatch_once(&onceToken, ^{// formatterDic = [NSMutableDictionary dictionaryWithCapacity:8];// });// // NSDateFormatter *dateFormatter_ = [formatterDic objectForKey:format];// if (dateFormatter_ == nil) {// @synchronized(formatterDic){// dateFormatter_ = [NSDateFormatter new];// dateFormatter_.dateFormat = format;// [formatterDic setObject:dateFormatter_ forKey:format];// // //可能需要设置一些默认属性,防止用户默认的设置造成时间转换问题,这个后续再看//// [dateFormatter_ setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]];//// [dateFormatter_ setLocale: [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];//// [dateFormatter_ setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];// }// // }// // return dateFormatter_; // 版本2 ,使用当前线程字典来保存对象 NSMutableDictionary *threadDic = [[NSThread currentThread] threadDictionary]; NSDateFormatter *dateFormatter = [threadDic objectForKey:format]; if (!dateFormatter) { dateFormatter = [[NSDateFormatter alloc]init]; dateFormatter.dateFormat = format; [threadDic setObject:dateFormatter forKey:format]; } return dateFormatter;}
上面就是这段代码逻辑,包含头文件,使用很简单:
NSDateFormatter *dateFormatter = [NSDateFormatter dateFormatterWithFormatString:@"MM/dd/yyyy"];
- NSDateFormatter使用思考
- NSDateFormatter使用
- NSDateFormatter的使用
- NSDateFormatter使用总结
- NSDate, NSDateFormatter的使用
- objc:NSDateFormatter使用备忘
- NSDateFormatter
- NSDateFormatter
- NSDateFormatter
- NSDateFormatter
- NSDateFormatter
- NSDateFormatter
- NSDateFormatter
- objc NSDateformatter使用小插曲
- iOS NSDateFormatter 日期的使用
- NSData 和NSDateFormatter的使用总结
- NSData 和NSDateFormatter的使用总结
- NSDate使用,NSDateFormatter转换,及DatePicker
- VS2012 + SWIG Python
- zoj 2588 Burning Bridges(求割边+对重边的处理)
- Mac 用RVM安装ruby 2.0.0的时候出现错误
- UVA 12298 Super Poker II (FFT + long double)
- stringstream转换整数是可能会加逗号
- NSDateFormatter使用思考
- 再次自由变换制作图形
- 在jquery中用下拉框列表显示默认的值
- Maven引入依赖后自动下载并关联源码(Source)
- 关于'\0'与0与NULL的一些实践
- Java线程同步中关键字synchronized详述
- 欢迎使用CSDN-markdown编辑器
- 正则
- nginx.conf配置说明