matomo-org / matomo-sdk-ios

Matomo iOS, tvOS and macOS SDK: a Matomo tracker written in Swift
MIT License
388 stars 164 forks source link

Event dispatching on background session #311

Closed simonnickel closed 2 years ago

simonnickel commented 5 years ago

Follow up on https://github.com/matomo-org/matomo-sdk-ios/issues/139

Instead of using URLSession.shared in URLSessionDispatcher a session configured for background should be used. This allows triggering the upload from the AppDelegates applicationDidEnterBackground().

Might need some more changes to URLSessionDispatcher. Seems like delegate methods need to be implemented to handle responses of background tasks. As well as handling of failing background tasks.

See https://developer.apple.com/documentation/foundation/urlsessionconfiguration/1407496-background Or some example implementation: https://www.raywenderlich.com/3244963-urlsession-tutorial-getting-started

simonnickel commented 5 years ago

Log output of failing requests.

MatomoTracker [W] Failed dispatching events with error Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x600001f89350 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=<...>, NSErrorFailingURLKey=<...>, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}

brototyp commented 4 years ago

Hi @simonnickel, I just created this PR. My approach there is to wrap every sending of the events into a background task. Can you try it out and give me some feedback?