-
Notifications
You must be signed in to change notification settings - Fork 17
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
Investigation: timers synchronization #33
base: master
Are you sure you want to change the base?
Changes from all commits
d5de41e
129ed8c
d3c2af4
2100b7b
b5677c7
716e2a7
a4aa2d5
ef92890
8b9d012
ee5c6a0
f2b47c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,7 @@ static void _DTXCFTimerTrampoline(CFRunLoopTimerRef timer, void *info) | |
// NSLog(@"❤️ %p", timer); | ||
|
||
id<DTXTimerProxy> tp = [DTXTimerSyncResource existingTimerProxyWithTimer:NS(timer)]; | ||
[tp fire:(__bridge NSTimer*)timer]; | ||
[tp fire]; | ||
} | ||
|
||
static CFRunLoopTimerRef (*__orig_CFRunLoopTimerCreate)(CFAllocatorRef allocator, CFAbsoluteTime fireDate, CFTimeInterval interval, CFOptionFlags flags, CFIndex order, CFRunLoopTimerCallBack callout, CFRunLoopTimerContext *context); | ||
|
@@ -72,6 +72,9 @@ CFRunLoopTimerRef __detox_sync_CFRunLoopTimerCreate(CFAllocatorRef allocator, CF | |
static CFRunLoopTimerRef (*__orig_CFRunLoopTimerCreateWithHandler)(CFAllocatorRef allocator, CFAbsoluteTime fireDate, CFTimeInterval interval, CFOptionFlags flags, CFIndex order, void (^block) (CFRunLoopTimerRef timer)); | ||
CFRunLoopTimerRef __detox_sync_CFRunLoopTimerCreateWithHandler(CFAllocatorRef allocator, CFAbsoluteTime fireDate, CFTimeInterval interval, CFOptionFlags flags, CFIndex order, void (^block) (id timer)) | ||
{ | ||
NSLog(@"🤔 CFRunLoopTimerCreateWithHandler"); | ||
|
||
// What is this doing? We don't seem to be creating a trampoline here - I feel like we should be? | ||
return (__bridge_retained CFRunLoopTimerRef)[[NSTimer alloc] initWithFireDate:CFBridgingRelease(CFDateCreate(allocator, fireDate)) interval:interval repeats:interval > 0 block:block]; | ||
} | ||
|
||
|
@@ -94,9 +97,9 @@ void __detox_sync_CFRunLoopRemoveTimer(CFRunLoopRef rl, CFRunLoopTimerRef timer, | |
// NSLog(@"🤦♂️ removeTimer: %@", NS(timer)); | ||
|
||
id<DTXTimerProxy> trampoline = [DTXTimerSyncResource existingTimerProxyWithTimer:NS(timer)]; | ||
[trampoline untrack]; | ||
|
||
__orig_CFRunLoopRemoveTimer(rl, timer, mode); | ||
|
||
[trampoline untrack]; | ||
Comment on lines
99
to
+102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've observed that By switching these, I was hoping that the timer would be cancelled (ensuring that the This doesn't seem to be the case, and I'm still seeing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Untracking the timer only after removing it from the runloop does make sense, but I'm not sure if it matters. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's very weird. 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe the thread where this timer was created and the thread where the timer was called to invalidate are different? Apple docs (invalidate()):
|
||
} | ||
|
||
static void (*__orig_CFRunLoopTimerInvalidate)(CFRunLoopTimerRef timer); | ||
|
@@ -105,9 +108,9 @@ void __detox_sync_CFRunLoopTimerInvalidate(CFRunLoopTimerRef timer) | |
// NSLog(@"🤦♂️ invalidate: %@", NS(timer)); | ||
|
||
id<DTXTimerProxy> trampoline = [DTXTimerSyncResource existingTimerProxyWithTimer:NS(timer)]; | ||
[trampoline untrack]; | ||
|
||
__orig_CFRunLoopTimerInvalidate(timer); | ||
|
||
[trampoline untrack]; | ||
} | ||
|
||
static void (*__orig___NSCFTimer_invalidate)(NSTimer* timer); | ||
|
@@ -116,11 +119,20 @@ void __detox_sync___NSCFTimer_invalidate(NSTimer* timer) | |
// NSLog(@"🤦♂️ invalidate: %@", timer); | ||
|
||
id<DTXTimerProxy> trampoline = [DTXTimerSyncResource existingTimerProxyWithTimer:timer]; | ||
[trampoline untrack]; | ||
|
||
__orig___NSCFTimer_invalidate(timer); | ||
|
||
[trampoline untrack]; | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
id<DTXTimerProxy> trampoline = [DTXTimerSyncResource existingTimerProxyWithTimer:self]; | ||
|
||
if(trampoline) { | ||
NSLog(@"🤦♂️ dealloc, but trampoline was still active: %@", self); | ||
[trampoline untrack]; | ||
} | ||
} | ||
Comment on lines
+127
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think adding this makes it safe to remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sense. |
||
|
||
+ (void)load | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,45 +113,45 @@ - (void)_untrackForParam:(NSUInteger*)param eventIdentifier:(NSString*(NS_NOESCA | |
- (void)trackViewNeedsLayout:(UIView *)view | ||
{ | ||
NSString* identifier = [self _trackForParam:&_viewNeedsLayoutCount eventDescription:_DTXStringReturningBlock(@"View Layout") objectDescription:_DTXStringReturningBlock(view.__detox_sync_safeDescription)]; | ||
|
||
__detox_sync_orig_dispatch_async(dispatch_get_main_queue(), ^ { | ||
[self _untrackForParam:&_viewNeedsLayoutCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
[self _untrackForParam:&self->_viewNeedsLayoutCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. being explicit in such cases is indeed better There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but except for being more explicit, is there any other goal in this change? What exactly is the fix here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, yes. Sorry. This fixes an XCode warning: |
||
}); | ||
} | ||
|
||
- (void)trackViewNeedsDisplay:(UIView *)view | ||
{ | ||
NSString* identifier = [self _trackForParam:&_viewNeedsDisplayCount eventDescription:_DTXStringReturningBlock(@"View Display") objectDescription:_DTXStringReturningBlock(view.__detox_sync_safeDescription)]; | ||
|
||
__detox_sync_orig_dispatch_async(dispatch_get_main_queue(), ^ { | ||
[self _untrackForParam:&_viewNeedsDisplayCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
[self _untrackForParam:&self->_viewNeedsDisplayCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
}); | ||
} | ||
|
||
- (void)trackLayerNeedsLayout:(CALayer *)layer | ||
{ | ||
NSString* identifier = [self _trackForParam:&_layerNeedsLayoutCount eventDescription:_DTXStringReturningBlock(@"Layer Layout") objectDescription:_DTXStringReturningBlock(layer.description)]; | ||
|
||
__detox_sync_orig_dispatch_async(dispatch_get_main_queue(), ^ { | ||
[self _untrackForParam:&_layerNeedsLayoutCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
[self _untrackForParam:&self->_layerNeedsLayoutCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
}); | ||
} | ||
|
||
- (void)trackLayerNeedsDisplay:(CALayer *)layer | ||
{ | ||
NSString* identifier = [self _trackForParam:&_layerNeedsDisplayCount eventDescription:_DTXStringReturningBlock(@"Layer Display") objectDescription:_DTXStringReturningBlock(layer.description)]; | ||
|
||
__detox_sync_orig_dispatch_async(dispatch_get_main_queue(), ^ { | ||
[self _untrackForParam:&_layerNeedsDisplayCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
[self _untrackForParam:&self->_layerNeedsDisplayCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
}); | ||
} | ||
|
||
- (void)trackLayerPendingAnimation:(CALayer*)layer | ||
{ | ||
NSString* identifier = [self _trackForParam:&_layerPendingAnimationCount eventDescription:_DTXStringReturningBlock(@"Layer Pending Animation") objectDescription:_DTXStringReturningBlock(layer.description)]; | ||
|
||
__detox_sync_orig_dispatch_async(dispatch_get_main_queue(), ^ { | ||
[self _untrackForParam:&_layerPendingAnimationCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
[self _untrackForParam:&self->_layerPendingAnimationCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
}); | ||
} | ||
|
||
|
@@ -160,9 +160,9 @@ - (void)trackViewControllerWillAppear:(UIViewController *)vc | |
if(vc.transitionCoordinator) | ||
{ | ||
NSString* identifier = [self _trackForParam:&_viewControllerWillAppearCount eventDescription:_DTXStringReturningBlock(@"View Layout") objectDescription:_DTXStringReturningBlock(vc.description)]; | ||
|
||
[vc.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { | ||
[self _untrackForParam:&_viewControllerWillAppearCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
[self _untrackForParam:&self->_viewControllerWillAppearCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
}]; | ||
} | ||
} | ||
|
@@ -172,9 +172,9 @@ - (void)trackViewControllerWillDisappear:(UIViewController *)vc | |
if(vc.transitionCoordinator) | ||
{ | ||
NSString* identifier = [self _trackForParam:&_viewControllerWillDisappearCount eventDescription:_DTXStringReturningBlock(@"View Layout") objectDescription:_DTXStringReturningBlock(vc.description)]; | ||
|
||
[vc.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { | ||
[self _untrackForParam:&_viewControllerWillDisappearCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
[self _untrackForParam:&self->_viewControllerWillDisappearCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
}]; | ||
} | ||
} | ||
|
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 don't understand why we're swizzling
CFRunLoopTimerCreateWithHandler
without adding any tracking for these timers (as far as I can tell).Unless
initWithFireDate
somehow ends up callingCFRunLoopTimerCreate
?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 happens a lot when running the Detox example app.
I don't understand what this is doing.
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.
Probably yes. This can be easily checked by putting a breakpoint on
__detox_sync_CFRunLoopTimerCreate
.https://github.com/wix/DetoxSync/tree/master/ExampleApp