mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
Keep references to observers to prevent garbage collection.
This commit is contained in:
@ -64,8 +64,10 @@ class IOSApplication implements definition.iOSApplication {
|
|||||||
private _delegate: typeof UIApplicationDelegate;
|
private _delegate: typeof UIApplicationDelegate;
|
||||||
private _currentOrientation = UIDevice.currentDevice().orientation;
|
private _currentOrientation = UIDevice.currentDevice().orientation;
|
||||||
private _window: Window;
|
private _window: Window;
|
||||||
|
private _observers: Array<NotificationObserver>;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
this._observers = new Array<NotificationObserver>();
|
||||||
this.addNotificationObserver(UIApplicationDidFinishLaunchingNotification, this.didFinishLaunchingWithOptions.bind(this));
|
this.addNotificationObserver(UIApplicationDidFinishLaunchingNotification, this.didFinishLaunchingWithOptions.bind(this));
|
||||||
this.addNotificationObserver(UIApplicationDidBecomeActiveNotification, this.didBecomeActive.bind(this));
|
this.addNotificationObserver(UIApplicationDidBecomeActiveNotification, this.didBecomeActive.bind(this));
|
||||||
this.addNotificationObserver(UIApplicationDidEnterBackgroundNotification, this.didEnterBackground.bind(this));
|
this.addNotificationObserver(UIApplicationDidEnterBackgroundNotification, this.didEnterBackground.bind(this));
|
||||||
@ -90,12 +92,17 @@ class IOSApplication implements definition.iOSApplication {
|
|||||||
public addNotificationObserver(notificationName: string, onReceiveCallback: (notification: NSNotification) => void): NotificationObserver {
|
public addNotificationObserver(notificationName: string, onReceiveCallback: (notification: NSNotification) => void): NotificationObserver {
|
||||||
var observer = NotificationObserver.new().initWithCallback(onReceiveCallback);
|
var observer = NotificationObserver.new().initWithCallback(onReceiveCallback);
|
||||||
NSNotificationCenter.defaultCenter().addObserverSelectorNameObject(observer, "onReceive", notificationName, null);
|
NSNotificationCenter.defaultCenter().addObserverSelectorNameObject(observer, "onReceive", notificationName, null);
|
||||||
|
this._observers.push(observer);
|
||||||
return observer;
|
return observer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public removeNotificationObserver(observer: any, notificationName: string) {
|
public removeNotificationObserver(observer: any, notificationName: string) {
|
||||||
|
var index = this._observers.indexOf(observer);
|
||||||
|
if (index >= 0) {
|
||||||
|
this._observers.splice(index, 1);
|
||||||
NSNotificationCenter.defaultCenter().removeObserverNameObject(observer, notificationName, null);
|
NSNotificationCenter.defaultCenter().removeObserverNameObject(observer, notificationName, null);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private didFinishLaunchingWithOptions(notification: NSNotification) {
|
private didFinishLaunchingWithOptions(notification: NSNotification) {
|
||||||
this._window = <Window>Window.alloc().initWithFrame(UIScreen.mainScreen().bounds);
|
this._window = <Window>Window.alloc().initWithFrame(UIScreen.mainScreen().bounds);
|
||||||
|
Reference in New Issue
Block a user