美文网首页
NSDateFormatter崩溃

NSDateFormatter崩溃

作者: OrrHsiao | 来源:发表于2023-04-18 10:14 被阅读0次

//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;
}

相关文章

网友评论

      本文标题:NSDateFormatter崩溃

      本文链接:https://www.haomeiwen.com/subject/ehuhjdtx.html