You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When handling multiple threads, the shared date formatter can provide a result different than expected due to one thread having a different date_format than another one and being performed at the same time.
The text was updated successfully, but these errors were encountered:
I've created a per-thread cache for NSDateFormatter.
It was published with 5d5f25a and available via cocoapods as 1.0.5.
Basically I save and retrive the object from inside the NSThread dictionary; this ensure you to get the instance relative to that thread called only once. It should be enough, tell me if you have problems.
privateclassfunc cachedObjectInCurrentThread<T:AnyObject>(key:String, create:()->T)->T{iflet threadDictionary =NSThread.currentThread().threadDictionary asNSMutableDictionary?{iflet cachedObject =threadDictionary[key]as!T?{return cachedObject
}else{letnewObject=create()threadDictionary[key]= newObject
return newObject
}}else{assert(false,"Current NSThread dictionary is nil. This should never happens, we will return a new instance of the object on each call")returncreate()}}privateclassfunc localThreadDateFormatter()->NSDateFormatter{returnNSDate.cachedObjectInCurrentThread("com.library.swiftdate.dateformatter"){letdateFormatter=NSDateFormatter()
dateFormatter.dateFormat ="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"return dateFormatter
}}
When handling multiple threads, the shared date formatter can provide a result different than expected due to one thread having a different date_format than another one and being performed at the same time.
The text was updated successfully, but these errors were encountered: