adzerk / adzerk-ios-sdk

Access Adzerk's ad serving APIs via iOS
https://www.adzerk.com
Other
9 stars 5 forks source link

AdzerkSDK requestPlacements method returns response in a worker thread #10

Closed AnttiLaitinenVincit closed 7 years ago

AnttiLaitinenVincit commented 7 years ago

The SDK seems to return results in a worker thread rather than the main thread. This is dangerous because it can cause e.g. race-conditions in non-threadsafe code (like the UI code) if the developer is unaware of this.

Code example:

let sdk = AdzerkSDK()
let placements: [ADZPlacement] = [ /* items */ ]

print(Thread.current) // prints main thread

sdk.requestPlacements(placements) { response in
  print(Thread.current) // prints worker thread
}

Currently a workaround is to wrap the completion handler with DispatchQueue.main.async to switch back to the main thread.

subdigital commented 7 years ago

I apologize for not seeing this sooner.

I agree, this is probably the most common use case for this type of request anyway. I've updated the SDK to version 1.0.2 which includes this change, as well as a way to specify your own queue in the initializer, should you need to.

I've also updated the podspec with this new version.

Thanks for reporting the issue!