8000 [camera]revert the weakSelf in FLTThreadSafeEventChannel (#6303) · flutter/plugins@e4cabdf · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit e4cabdf

Browse files
authored
[camera]revert the weakSelf in FLTThreadSafeEventChannel (#6303)
1 parent 38cd9af commit e4cabdf

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

packages/camera/camera_avfoundation/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.9.8+5
2+
3+
* Fixes a regression introduced in 0.9.8+4 where the stream handler is not set.
4+
15
## 0.9.8+4
26

37
* Fixes a crash due to sending orientation change events when the engine is torn down.

packages/camera/camera_avfoundation/example/ios/RunnerTests/ThreadSafeEventChannelTests.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,20 @@ - (void)testSetStreamHandler_shouldDispatchToMainThreadIfCalledFromBackgroundThr
6363
[self waitForExpectationsWithTimeout:1 handler:nil];
6464
}
6565

66+
- (void)testEventChannel_shouldBeKeptAliveWhenDispatchingBackToMainThread {
67+
XCTestExpectation *expectation =
68+
[self expectationWithDescription:@"Completion should be called."];
69+
dispatch_async(dispatch_queue_create("test", NULL), ^{
70+
FLTThreadSafeEventChannel *channel = [[FLTThreadSafeEventChannel alloc]
71+
initWithEventChannel:OCMClassMock([FlutterEventChannel class])];
72+
73+
[channel setStreamHandler:OCMOCK_ANY
74+
completion:^{
75+
[expectation fulfill];
76+
}];
77+
});
78+
79+
[self waitForExpectationsWithTimeout:1 handler:nil];
80+
}
81+
6682
@end

packages/camera/camera_avfoundation/ 8000 ios/Classes/FLTThreadSafeEventChannel.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ - (instancetype)initWithEventChannel:(FlutterEventChannel *)channel {
2121

2222
- (void)setStreamHandler:(NSObject<FlutterStreamHandler> *)handler
2323
completion:(void (^)(void))completion {
24-
__weak typeof(self) weakSelf = self;
24+
// WARNING: Should not use weak self, because FLTThreadSafeEventChannel is a local variable
25+
// (retained within call stack, but not in the heap). FLTEnsureToRunOnMainQueue may trigger a
26+
// context switch (when calling from background thread), in which case using weak self will always
27+
// result in a nil self. Alternative to using strong self, we can also create a local strong
28+
// variable to be captured by this block.
2529
FLTEnsureToRunOnMainQueue(^{
26-
typeof(self) strongSelf = weakSelf;
27-
if (!strongSelf) return;
28-
[strongSelf.channel setStreamHandler:handler];
30+
[self.channel setStreamHandler:handler];
2931
completion();
3032
});
3133
}

packages/camera/camera_avfoundation/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: camera_avfoundation
22
description: iOS implementation of the camera plugin.
33
repository: https://github.com/flutter/plugins/tree/main/packages/camera/camera_avfoundation
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
5-
version: 0.9.8+4
5+
version: 0.9.8+5
66

77
environment:
88
sdk: ">=2.14.0 <3.0.0"

0 commit comments

Comments
 (0)
0