Open danielgarbien opened 1 year ago
Here's a similar issue from 2018 triggered by updating UserDefaults
on a background thread.
There, it was suggested to swizzle UserDefaults
method and run the change on the main thread. @amit-kremer93 is it still the way to go?
@andr-ggn, does the workaround you proposed here still applies?
This is my workaround:
extension UserDefaults {
static func swizzleInstanceMethodsForAppFlyerWorkaround() {
let originalSelector = #selector(set(_:forKey:) as (UserDefaults) -> (Any?, String) -> Void)
let swizzledSelector = #selector(swizzled_set(_:forKey:))
guard let originalMethod = class_getInstanceMethod(self, originalSelector),
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
else {
assertionFailure("Not able to swizzle UserDefaults methods.")
return
}
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
private extension UserDefaults {
@objc func swizzled_set(_ value: Any?, forKey key: String) {
let appsFlyerInAppCounterKey = "AppsFlyerInAppCounter"
guard
!Thread.isMainThread,
key == appsFlyerInAppCounterKey,
let intValue = value as? Int
else {
swizzled_set(value, forKey: key)
return
}
let diff = intValue - integer(forKey: appsFlyerInAppCounterKey)
DispatchQueue.main.async {
let newValue = self.integer(forKey: appsFlyerInAppCounterKey) + diff
self.swizzled_set(newValue, forKey: key)
}
}
}
Report
SDK Version
6.10.0
What did you do?
Included AppsFlyerLib in the iOS app.
What did you expect to happen?
App kept working.
What happened instead?
App occasionally crashes.
Please provide any other relevant information.