8000 fix(ios): proper disposal and recreation of iOS native views (#9879) · NativeScript/NativeScript@d2d6a0b · GitHub
[go: up one dir, main page]

Skip to content

Commit d2d6a0b

Browse files
CatchABusNathanWalker
authored andcommitted
fix(ios): proper disposal and recreation of iOS native views (#9879)
1 parent bff9575 commit d2d6a0b

File tree

5 files changed

+36
-19
lines changed

5 files changed

+36
-19
lines changed

apps/automated/src/ui/lifecycle/lifecycle-tests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as helper from '../../ui-helper';
22
import * as btnCounter from './pages/button-counter';
33
import * as TKUnit from '../../tk-unit';
4-
import { isIOS, isAndroid } from '@nativescript/core';
4+
import { isIOS } from '@nativescript/core';
55

66
// Integration tests that asser sertain runtime behavior, lifecycle events atc.
77

@@ -163,7 +163,7 @@ export function test_css_sets_properties() {
163163
page.content = stack;
164164

165165
// TODO: The check counts here should be the same as the counts before removing from the page.
166-
const expectedNativeSettersAfterReaddedToPage = isAndroid ? [2, 4, 4, 4] : expectedChangesAfterResettingClasses;
166+
const expectedNativeSettersAfterReaddedToPage = [2, 4, 4, 4];
167167
for (let i = 0; i < buttons.length; i++) {
168168
TKUnit.assertEqual(buttons[i].colorSetNativeCount, expectedNativeSettersAfterReaddedToPage[i], `Expected ${buttons[i].id} native set to not be called when added to page.`);
169169
TKUnit.assertEqual(buttons[i].colorPropertyChangeCount, expectedChangesAfterResettingClasses[i], `Expected ${buttons[i].id} change notifications for css properties to not occur when added to page.`);

packages/core/ui/animation/animation-interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,5 @@ export interface AnimationDefinitionInternal extends AnimationDefinition {
6969
export interface IOSView extends View {
7070
_suspendPresentationLayerUpdates();
7171
_resumePresentationLayerUpdates();
72-
_isPresentationLayerUpdateSuspeneded();
72+
_isPresentationLayerUpdateSuspended();
7373
}

packages/core/ui/core/view-base/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,7 @@ export abstract class ViewBase extends Observable implements ViewBaseDefinition
909909

910910
public destroyNode(forceDestroyChildren?: boolean): void {
911911
this.reusable = false;
912+
this.callUnloaded();
912913
this._tearDownUI(forceDestroyChildren);
913914
}
914915

@@ -958,12 +959,9 @@ export abstract class ViewBase extends Observable implements ViewBaseDefinition
958959

959960
this._suspendNativeUpdates(SuspendType.UISetup);
960961

961-
if (global.isAndroid) {
962-
this.setNativeView(null);
963-
this._androidView = null;
964-
}
965-
966-
// this._iosView = null;
962+
this.setNativeView(null);
963+
this._androidView = null;
964+
this._iosView = null;
967965

968966
this._context = null;
969967
}

packages/core/ui/core/view/index.ios.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export class View extends ViewCommon implements ViewDefinition {
3737
*/
3838
private _modalAnimatedOptions: Array<boolean>;
3939
private _isLaidOut = false;
40-
private _hasTransfrom = false;
40+
private _hasTransform = false;
41+
private _hasPendingTransform = false;
4142
private _privateFlags: number = PFLAG_LAYOUT_REQUIRED | PFLAG_FORCE_LAYOUT;
4243
private _cachedFrame: CGRect;
4344
private _suspendCATransaction = false;
@@ -63,6 +64,15 @@ export class View extends ViewCommon implements ViewDefinition {
6364
this.once(View.loadedEvent, () => setupAccessibleView(this));
6465
}
6566

67+
disposeNativeView() {
68+
super.disposeNativeView();
69+
70+
this._cachedFrame = null;
71+
this._isLaidOut = false;
72+
this._hasTransform = false;
73+
this._hasPendingTransform = false;
74+
}
75+
6676
public requestLayout(): void {
6777
super.requestLayout();
6878
this._privateFlags |= PFLAG_FORCE_LAYOUT;
@@ -119,6 +129,10 @@ export class View extends ViewCommon implements ViewDefinition {
119129
}
120130

121131
this.updateBackground(sizeChanged);
132+
if (this._hasPendingTransform) {
133+
this.updateNativeTransform();
134+
this._hasPendingTransform = false;
135+
}
122136
this._privateFlags &= ~PFLAG_FORCE_LAYOUT;
123137
}
124138

@@ -175,7 +189,7 @@ export class View extends ViewCommon implements ViewDefinition {
175189
this._cachedFrame = frame;
176190
let adjustedFrame = null;
177191
let transform = null;
178-
if (this._hasTransfrom) {
192+
if (this._hasTransform) {
179193
// Always set identity transform before setting frame;
180194
transform = nativeView.layer.transform;
181195
nativeView.layer.transform = CATransform3DIdentity;
@@ -189,7 +203,7 @@ export class View extends ViewCommon implements ViewDefinition {
189203
nativeView.frame = adjustedFrame;
190204
}
191205

192-
if (this._hasTransfrom) {
206+
if (this._hasTransform) {
193207
// re-apply the transform after the frame is adjusted
194208
nativeView.layer.transform = transform;
195209
}
@@ -363,6 +377,11 @@ export class View extends ViewCommon implements ViewDefinition {
363377
}
364378

365379
public updateNativeTransform() {
380+
if (!this.isLayoutValid) {
381+
this._hasPendingTransform = true;
382+
return;
383+
}
384+
366385
const scaleX = this.scaleX || 1e-6;
367386
const scaleY = this.scaleY || 1e-6;
368387
const perspective = this.perspective || 300;
@@ -378,12 +397,12 @@ export class View extends ViewCommon implements ViewDefinition {
378397
transform = iOSNativeHelper.applyRotateTransform(transform, this.rotateX, this.rotateY, this.rotate);
379398
transform = CATransform3DScale(transform, scaleX, scaleY, 1);
380399
if (!CATransform3DEqualToTransform(this.nativeViewProtected.layer.transform, transform)) {
381-
const updateSuspended = this._isPresentationLayerUpdateSuspeneded();
400+
const updateSuspended = this._isPresentationLayerUpdateSuspended();
382401
if (!updateSuspended) {
383402
CATransaction.begin();
384403
}
385404
this.nativeViewProtected.layer.transform = transform;
386-
this._hasTransfrom = this.nativeViewProtected && !CATransform3DEqualToTransform(this.nativeViewProtected.transform3D, CATransform3DIdentity);
405+
this._hasTransform = this.nativeViewProtected && !CATransform3DEqualToTransform(this.nativeViewProtected.transform3D, CATransform3DIdentity);
387406
if (!updateSuspended) {
388407
CATransaction.commit();
389408
}
@@ -409,7 +428,7 @@ export class View extends ViewCommon implements ViewDefinition {
409428
this._suspendCATransaction = false;
410429
}
411430

412-
public _isPresentationLayerUpdateSuspeneded(): boolean {
431+
public _isPresentationLayerUpdateSuspended(): boolean {
413432
return this._suspendCATransaction || this._suspendNativeUpdatesCount > 0;
414433
}
415434

@@ -689,7 +708,7 @@ export class View extends ViewCommon implements ViewDefinition {
689708
}
690709
[opacityProperty.setNative](value: number) {
691710
const nativeView = this.nativeViewProtected;
692-
const updateSuspended = this._isPresentationLayerUpdateSuspeneded();
711+
const updateSuspended = this._isPresentationLayerUpdateSuspended();
693712
if (!updateSuspended) {
694713
CATransaction.begin();
695714
}
@@ -845,7 +864,7 @@ export class View extends ViewCommon implements ViewDefinition {
845864
}
846865

847866
_redrawNativeBackground(value: UIColor | Background): void {
848-
const updateSuspended = this._isPresentationLayerUpdateSuspeneded();
867+
const updateSuspended = this._isPresentationLayerUpdateSuspended();
849868
if (!updateSuspended) {
850869
CATransaction.begin();
851870
}

packages/core/ui/core/view/view-helper/index.ios.ts

Lines changed: 2 additions & 2 deletions
< 55F td data-grid-cell-id="diff-83c9fab79b1de3837e713014b8b8a21b7c428d1efbd5b0e36f3aba51cf3b4db4-112-111-1" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-deletionNum-bgColor, var(--diffBlob-deletion-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ class UILayoutViewController extends UIViewController {
101101

102102
IOSHelper.updateAutoAdjustScrollInsets(this, owner);
103103

104-
if (!owner.parent) {
104+
if (!owner.isLoaded && !owner.parent) {
105105
owner.callLoaded();
106106
}
107107
}
108108

109109
public viewDidDisappear(animated: boolean): void {
110110
super.viewDidDisappear(animated);
111111
const owner = this.owner.get();
112-
if (owner && !owner.parent) {
112+
if (owner && owner.isLoaded && !owner.parent) {
113113
owner.callUnloaded();
114114
}
115115
}

0 commit comments

Comments
 (0)
0