Is OCMapper thread-safe?
Hi,
I've been using OCMapper for several weeks now and I'm a big fan! While there's a few things I'd love to see improved - in general it was the best JSON <-> Object library I could find for iOS.
However, every once and a while I noticed my app is crashing and the stack trace points inside of OCMapper. It's very difficult to reproduce which makes the crash even worse in my mind.
But, my theory is that OCMapper isn't thread-safe. We have a thread pool handling responses from the server and OCMapper processes everything as a singleton.
Is this a known limitation? I can handle the results in a single thread to be safe as well but wanted to know if it's supposed to be thread-safe and the issue is elsewhere in my code.
@interface ObjectMapper()
@property (nonatomic, strong) NSMutableArray *commonDateFormaters;
@property (nonatomic, strong) NSMutableDictionary *mappedClassNames;
@property (nonatomic, strong) NSMutableDictionary *mappedPropertyNames;
@property (nonatomic, strong) NSMutableArray *instanceProviders;
@end
Hi @jpage4500
I've never had any issues with threading. I'm using OCMapper in my current app that has 100k sessions, millions of HTTP requests a day. OCMapper is always running on the global queue. I'll write some tests and try to map object on as many threads as possible to be sure, but I doubt that threading is your issue.
Do you happen to have the crash logs?
I have something similar to this
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
guard let json = json else {
dispatch_async(dispatch_get_main_queue()) {
completeWithError(NetworkErrorType.UnexpectedEmptyResponse)
}
return
}
let convertedObject = ObjectMapper.sharedInstance().objectFromSource(json, toInstanceOfClass: type) as? T
dispatch_async(dispatch_get_main_queue()) {
completion(SingleNetworkResponse<T>(data: convertedObject, response: response))
}
}