//NSDateFormatter为不安全线程,当多个线程同时访问1个NSDateFormatter对象时,有可能会Crash
//1.增加线程安全处理
//2.为每一个线程创建对应的NSDateFormatter
- (NSDateFormatter *)df {
if (!_df) {
NSString *keyName = @"logDateFormatter";
NSMutableDictionary *threadDic = [[NSThread currentThread] threadDictionary];
NSDateFormatter *df = threadDic[keyName];
@synchronized (self) {
if (!df) {
df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
threadDic[keyName] = df;
_df = df;
}
}
}
return _df;
}
网友评论