Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix dispatchSource #1344
Fix dispatchSource #1344
Changes from 9 commits
f031476
c5ef607
452da07
0c8f6f0
653ba4e
5b9a8bd
2aa87fe
59bf80c
980cc21
ae55b24
d4bb6aa
3be7fd8
042f79b
6442b3e
14a2ed8
2346c60
f7342c5
40eeac7
0345c26
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we've separate out this logic can we remove the
timeInterval
property fromBTAnalyticsService
and set the default in this class? That way the timer logic is fully contained here and we are only instantiating it inBTAnalyticsService
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeh, we can just hard code it to 15 in the RepeatingTimer class. BTAnalyticsService doesn't need to change the interval
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@richherrera wanted to make sure you saw this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By removing
timeInterval
from classBTAnalyticsService
and setting a default value in this class, we're mixing the "logic" again. Imagine we want to change the time interval from 15 seconds to 30, like in Android. I would expect to make this change inBTAnalyticsService
, notRepeatingTimer
. What do you think? Any ideas? What I came up with still looks like a magic number:Any suggestions on how to set the value 15 without it looking like a
magic number
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure that I agree with this.
BTAnalyticsService
only responsibility should be sending events, it should not care about how often we send events at all. On the other hand theRepeatingTimer
should control all time related logic including the interval of the timer. I don't thinkBTAnalyticsService
needs to care how often events are sent only that it's sending events.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I completely agree with you. I was thinking more about making this class usable in multiple places if needed, considering that in some other context, 15 seconds might be too short. But I'm happy to make the change. Do you think it would be good to put it in the initializer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious why we need to manage this state ourselves?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GCD timers can be somewhat sensitive. If you try and
resume
/suspend
an alreadyresumed
/suspended
timer, you will get a crash with a reason like:This tells us we have tried to resume an already resumed timer. We just need to balance the calls to suspend and resume, like the documentation states:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, thanks for the background here. This makes sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we include that link & those details in a comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, added: 3be7fd8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is great work Rich. I am wondering if we want to synchronize resume and suspends as well since analytics could be sent from different threads. We could use actor which would require awaits on function calls but we could also use serial dispatch queue. wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent suggestion. For now, I think
DispatchSource
is a task that runs periodically, so we could proceed without needing actor's. Also, based on Jax's implementation, theDispatchSource
is created to run on the main thread. From what I see, the task of sending analytics can be on different threads, but both only enqueue events. The task of sending them is handled by the single instance ofDispatchSource
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. I didn't know that this line
let timerSource = DispatchSource.makeTimerSource(queue: DispatchQueue.main)
ensures that the handler is run on main as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick - we try to name tests like this format:
test<MethodName>_{optional when<Condition>}_<ExpectedResult>
This test might be more readable as two separate tests. For ex:
testResume_callsEventHandler
ortestSuspend_doesNotCallEventHandler
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for let me know, updated here: d4bb6aa