Fix issue viewdidload call while init FlutterViewController (flutter/engine#16672)

Co-authored-by: Aaron Clarke <aaclarke@google.com>
This commit is contained in:
Duong Nguyen
2020-03-07 00:35:40 +07:00
committed by GitHub
parent 64beefcc3a
commit 56309c96cf
6 changed files with 52 additions and 15 deletions

View File

@@ -224,6 +224,10 @@ NSString* const FlutterDefaultDartEntrypoint = nil;
}
}
- (void)attachView {
self.iosPlatformView->attachView();
}
- (void)setFlutterViewControllerWillDeallocObserver:(id<NSObject>)observer {
if (observer != _flutterViewControllerWillDeallocObserver) {
if (_flutterViewControllerWillDeallocObserver) {

View File

@@ -44,6 +44,7 @@
- (FlutterTextInputPlugin*)textInputPlugin;
- (void)launchEngine:(NSString*)entrypoint libraryURI:(NSString*)libraryOrNil;
- (BOOL)createShell:(NSString*)entrypoint libraryURI:(NSString*)libraryOrNil;
- (void)attachView;
@end

View File

@@ -552,8 +552,8 @@ static void sendFakeTouchEvent(FlutterEngine* engine,
#pragma mark - UIViewController lifecycle notifications
- (void)viewWillAppear:(BOOL)animated {
TRACE_EVENT0("flutter", "viewWillAppear");
- (void)viewDidLoad {
TRACE_EVENT0("flutter", "viewDidLoad");
if (_engineNeedsLaunch) {
[_engine.get() launchEngine:nil libraryURI:nil];
@@ -561,6 +561,16 @@ static void sendFakeTouchEvent(FlutterEngine* engine,
_engineNeedsLaunch = NO;
}
FML_DCHECK([_engine.get() viewController] != nil)
<< "FlutterViewController::viewWillAppear:AttachView ViewController was nil";
[_engine.get() attachView];
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated {
TRACE_EVENT0("flutter", "viewWillAppear");
// Send platform settings to Flutter, e.g., platform brightness.
[self onUserSettingsChanged:nil];

View File

@@ -11,6 +11,10 @@
FLUTTER_ASSERT_ARC
@interface FlutterEngine ()
- (BOOL)createShell:(NSString*)entrypoint libraryURI:(NSString*)libraryURI;
@end
extern NSNotificationName const FlutterViewControllerWillDealloc;
/// A simple mock class for FlutterEngine.
@@ -457,4 +461,16 @@ typedef enum UIAccessibilityContrast : NSInteger {
[self waitForExpectations:@[ expectation ] timeout:1.0];
}
- (void)testDoesntLoadViewInInit {
XCTestExpectation* expectation =
[[XCTestExpectation alloc] initWithDescription:@"notification called"];
FlutterDartProject* project = [[FlutterDartProject alloc] init];
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"foobar" project:project];
[engine createShell:@"" libraryURI:@""];
FlutterViewController* realVC = [[FlutterViewController alloc] initWithEngine:engine
nibName:nil
bundle:nil];
XCTAssertFalse([realVC isViewLoaded], @"shouldn't have loaded since it hasn't been shown");
}
@end

View File

@@ -34,6 +34,7 @@ class PlatformViewIOS final : public PlatformView {
fml::WeakPtr<FlutterViewController> GetOwnerViewController() const;
void SetOwnerViewController(fml::WeakPtr<FlutterViewController> owner_controller);
void attachView();
void RegisterExternalTexture(int64_t id, NSObject<FlutterTexture>* texture);

View File

@@ -64,20 +64,25 @@ void PlatformViewIOS::SetOwnerViewController(fml::WeakPtr<FlutterViewController>
owner_controller_.reset();
}] retain]);
if (owner_controller_) {
ios_surface_ =
[static_cast<FlutterView*>(owner_controller.get().view) createSurface:gl_context_];
FML_DCHECK(ios_surface_ != nullptr);
if (owner_controller_ && [owner_controller_.get() isViewLoaded]) {
this->attachView();
}
// Do not call `NotifyCreated()` here - let FlutterViewController take care
// of that when its Viewport is sized. If `NotifyCreated()` is called here,
// it can occasionally get invoked before the viewport is sized resulting in
// a framebuffer that will not be able to completely attach.
}
if (accessibility_bridge_) {
accessibility_bridge_.reset(
new AccessibilityBridge(static_cast<FlutterView*>(owner_controller_.get().view), this,
[owner_controller.get() platformViewsController]));
}
// Do not call `NotifyCreated()` here - let FlutterViewController take care
// of that when its Viewport is sized. If `NotifyCreated()` is called here,
// it can occasionally get invoked before the viewport is sized resulting in
// a framebuffer that will not be able to completely attach.
void PlatformViewIOS::attachView() {
FML_DCHECK(owner_controller_);
ios_surface_ =
[static_cast<FlutterView*>(owner_controller_.get().view) createSurface:gl_context_];
FML_DCHECK(ios_surface_ != nullptr);
if (accessibility_bridge_) {
accessibility_bridge_.reset(
new AccessibilityBridge(static_cast<FlutterView*>(owner_controller_.get().view), this,
[owner_controller_.get() platformViewsController]));
}
}