Commit ef1f8f2d authored by 余玠's avatar 余玠

实现通过FlutterBoost销毁engine

parent aa8d47d6
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import <flutter_boost/FlutterBoost.h> #import <flutter_boost/FlutterBoost.h>
@interface AppDelegate : FLBFlutterAppDelegate <UIApplicationDelegate> @interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end @end
...@@ -19,12 +19,6 @@ ...@@ -19,12 +19,6 @@
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ {
PlatformRouterImp *router = [PlatformRouterImp new];
[FlutterBoostPlugin.sharedInstance startFlutterWithPlatform:router
onStart:^(FlutterEngine *engine) {
}];
self.window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds]; self.window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
...@@ -35,17 +29,14 @@ ...@@ -35,17 +29,14 @@
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"hybrid" image:nil tag:0]; vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"hybrid" image:nil tag:0];
FLBFlutterViewContainer *fvc = FLBFlutterViewContainer.new; UIViewController *fvc = UIViewController.new;
[fvc setName:@"tab" params:@{}]; // [fvc setName:@"tab" params:@{}];
fvc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"flutter_tab" image:nil tag:1]; fvc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"flutter_tab" image:nil tag:1];
UITabBarController *tabVC = [[UITabBarController alloc] init]; UITabBarController *tabVC = [[UITabBarController alloc] init];
UINavigationController *rvc = [[UINavigationController alloc] initWithRootViewController:tabVC]; UINavigationController *rvc = [[UINavigationController alloc] initWithRootViewController:tabVC];
router.navigationController = rvc;
tabVC.viewControllers = @[vc,fvc]; tabVC.viewControllers = @[vc,fvc];
self.window.rootViewController = rvc; self.window.rootViewController = rvc;
...@@ -57,16 +48,48 @@ ...@@ -57,16 +48,48 @@
UIButton *nativeButton = [UIButton buttonWithType:UIButtonTypeCustom]; UIButton *nativeButton = [UIButton buttonWithType:UIButtonTypeCustom];
nativeButton.frame = CGRectMake(self.window.frame.size.width * 0.5 - 50, 200, 100, 45); nativeButton.frame = CGRectMake(self.window.frame.size.width * 0.5 - 60, 150, 130, 45);
nativeButton.backgroundColor = [UIColor redColor]; nativeButton.backgroundColor = [UIColor redColor];
[nativeButton setTitle:@"push native" forState:UIControlStateNormal]; [nativeButton setTitle:@"push native" forState:UIControlStateNormal];
[nativeButton addTarget:self action:@selector(pushNative) forControlEvents:UIControlEventTouchUpInside]; [nativeButton addTarget:self action:@selector(pushNative) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:nativeButton]; [self.window addSubview:nativeButton];
UIButton *startEngine = [UIButton buttonWithType:UIButtonTypeCustom];
startEngine.frame = CGRectMake(self.window.frame.size.width * 0.5 - 60, 230, 130, 40);
startEngine.backgroundColor = [UIColor redColor];
[startEngine setTitle:@"start engine" forState:UIControlStateNormal];
[startEngine addTarget:self action:@selector(startFlutter:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:startEngine];
UIButton *destroyEngine = [UIButton buttonWithType:UIButtonTypeCustom];
destroyEngine.frame = CGRectMake(self.window.frame.size.width * 0.5 - 60, 300, 130, 40);
destroyEngine.backgroundColor = [UIColor redColor];
[destroyEngine setTitle:@"destroy engine" forState:UIControlStateNormal];
[destroyEngine addTarget:self action:@selector(destroyFlutter:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:destroyEngine];
return YES; return YES;
} }
- (void)startFlutter:(id)sender {
PlatformRouterImp *router = [PlatformRouterImp new];
router.navigationController = (UINavigationController*)self.window.rootViewController;
[FlutterBoostPlugin.sharedInstance startFlutterWithPlatform:router
onStart:^(FlutterEngine *engine) {
}];
}
- (void)destroyFlutter:(id)sender {
//切记:在destroyEngine前务必将所有FlutterViewController及其子类的实例销毁。在这里是FLBFlutterViewContainer。否则会异常;以下是全部步骤
//1. 首先通过为所有FlutterPlugin的methodChannel属性设为nil来接触其与FlutterEngine的间接强引用
//2. 销毁所有的FlutterViewController实例,来解除其与FlutterEngine的强引用
//3. 调用FlutterBoostPlugin.destroyEngine函数来解除与FlutterEngine的强引用
[FlutterBoostPlugin.sharedInstance destroyEngine];
}
- (void)pushNative - (void)pushNative
{ {
UINavigationController *nvc = (id)self.window.rootViewController; UINavigationController *nvc = (id)self.window.rootViewController;
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES"> <document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait"> <device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/> <adaptation id="fullscreen"/>
</device> </device>
<dependencies> <dependencies>
<deployment identifier="iOS"/> <deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/> <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies> </dependencies>
<objects> <objects>
......
...@@ -108,4 +108,6 @@ ...@@ -108,4 +108,6 @@
exts:(NSDictionary *)exts exts:(NSDictionary *)exts
onPageFinished:(void (^)(NSDictionary *))resultCallback onPageFinished:(void (^)(NSDictionary *))resultCallback
completion:(void (^)(BOOL))completion; completion:(void (^)(BOOL))completion;
- (void)destroyEngine;
@end @end
...@@ -118,8 +118,8 @@ ...@@ -118,8 +118,8 @@
__weak __typeof__(self) weakSelf = self; __weak __typeof__(self) weakSelf = self;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
__strong __typeof__(weakSelf) self = weakSelf; __strong __typeof__(weakSelf) self = weakSelf;
self.factory = FLBFactory.new; FLBFactory *factory = FLBFactory.new;
self.application = [self->_factory createApplication:platform]; self.application = [factory createApplication:platform];
[self.application startFlutterWithPlatform:platform [self.application startFlutterWithPlatform:platform
onStart:callback]; onStart:callback];
}); });
...@@ -169,4 +169,9 @@ ...@@ -169,4 +169,9 @@
id<FLBFlutterApplicationInterface> app = [[FlutterBoostPlugin sharedInstance] application]; id<FLBFlutterApplicationInterface> app = [[FlutterBoostPlugin sharedInstance] application];
[app close:uniqueId result:resultData exts:exts completion:completion]; [app close:uniqueId result:resultData exts:exts completion:completion];
} }
- (void)destroyEngine{
self.methodChannel = nil;
self.application = nil;
}
@end @end
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
#import "FlutterBoostPlugin.h" #import "FlutterBoostPlugin.h"
@interface FlutterBoostPlugin() @interface FlutterBoostPlugin()
@property (nonatomic,strong) id<FLBFlutterApplicationInterface> application; @property (nonatomic,strong) id<FLBFlutterApplicationInterface> application;
@property (nonatomic,strong) id<FLBAbstractFactory> factory;
@property (nonatomic,strong) FlutterMethodChannel *methodChannel; @property (nonatomic,strong) FlutterMethodChannel *methodChannel;
@property (nonatomic,copy) NSString *fPageId; @property (nonatomic,copy) NSString *fPageId;
@property (nonatomic,copy) NSString *fPagename; @property (nonatomic,copy) NSString *fPagename;
......
...@@ -101,11 +101,6 @@ ...@@ -101,11 +101,6 @@
arguments:nil]; arguments:nil];
} }
- (FlutterEngine *)engine
{
return _engine;
}
- (void)atacheToViewController:(FlutterViewController *)vc - (void)atacheToViewController:(FlutterViewController *)vc
{ {
if(_engine.viewController != vc){ if(_engine.viewController != vc){
...@@ -128,5 +123,15 @@ ...@@ -128,5 +123,15 @@
// [self detach]; // [self detach];
} }
- (void)dealloc{
[self.engine setViewController:nil];
[self.engine destroyContext];
__weak __typeof__(_engine) weak = _engine;
NSLog(@"Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)weak));
//// CFRelease((__bridge CFTypeRef)_engine);
// _engine = nil;
// NSLog(@"after Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)weak));
// NSLog(@"[DEBUG]---dealloc--");
}
@end @end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment