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

Skip to content

fix(ios): proper disposal and recreation of iOS native views #9879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
10000 Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/automated/src/ui/lifecycle/lifecycle-tests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as helper from '../../ui-helper';
import * as btnCounter from './pages/button-counter';
import * as TKUnit from '../../tk-unit';
import { isIOS, isAndroid } from '@nativescript/core';
import { isIOS } from '@nativescript/core';

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

Expand Down Expand Up @@ -163,7 +163,7 @@ export function test_css_sets_properties() {
page.content = stack;

// TODO: The check counts here should be the same as the counts before removing from the page.
const expectedNativeSettersAfterReaddedToPage = isAndroid ? [2, 4, 4, 4] : expectedChangesAfterResettingClasses;
const expectedNativeSettersAfterReaddedToPage = [2, 4, 4, 4];
for (let i = 0; i < buttons.length; i++) {
TKUnit.assertEqual(buttons[i].colorSetNativeCount, expectedNativeSettersAfterReaddedToPage[i], `Expected ${buttons[i].id} native set to not be called when added to page.`);
TKUnit.assertEqual(buttons[i].colorPropertyChangeCount, expectedChangesAfterResettingClasses[i], `Expected ${buttons[i].id} change notifications for css properties to not occur when added to page.`);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/core",
"version": "8.2.3",
"version": "8.2.4-alpha.0",
"description": "A JavaScript library providing an easy to use api for interacting with iOS and Android platform APIs.",
"main": "index",
"types": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/ui/animation/animation-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ export interface AnimationDefinitionInternal extends AnimationDefinition {
export interface IOSView extends View {
_suspendPresentationLayerUpdates();
_resumePresentationLayerUpdates();
_isPresentationLayerUpdateSuspeneded();
_isPresentationLayerUpdateSuspended();
}
10 changes: 4 additions & 6 deletions packages/core/ui/core/view-base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,7 @@ export abstract class ViewBase extends Observable implements ViewBaseDefinition

public destroyNode(forceDestroyChildren?: boolean): void {
this.reusable = false;
this.callUnloaded();
this._tearDownUI(forceDestroyChildren);
}

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

this._suspendNativeUpdates(SuspendType.UISetup);

if (global.isAndroid) {
this.setNativeView(null);
this._androidView = null;
}

// this._iosView = null;
this.setNativeView(null);
this._androidView = null;
this._iosView = null;

this._context = null;
}
Expand Down
35 changes: 27 additions & 8 deletions packages/core/ui/core/view/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export class View extends ViewCommon implements ViewDefinition {
*/
private _modalAnimatedOptions: Array<boolean>;
private _isLaidOut = false;
private _hasTransfrom = false;
private _hasTransform = false;
private _hasPendingTransform = false;
private _privateFlags: number = PFLAG_LAYOUT_REQUIRED | PFLAG_FORCE_LAYOUT;
private _cachedFrame: CGRect;
private _suspendCATransaction = false;
Expand All @@ -63,6 +64,15 @@ export class View extends ViewCommon implements ViewDefinition {
this.once(View.loadedEvent, () => setupAccessibleView(this));
}

disposeNativeView() {
super.disposeNativeView();

this._cachedFrame = null;
this._isLaidOut = false;
this._hasTransform = false;
this._hasPendingTransform = false;
}

public requestLayout(): void {
super.requestLayout();
this._privateFlags |= PFLAG_FORCE_LAYOUT;
Expand Down Expand Up @@ -119,6 +129,10 @@ export class View extends ViewCommon implements ViewDefinition {
}

this.updateBackground(sizeChanged);
if (this._hasPendingTransform) {
this.updateNativeTransform();
this._hasPendingTransform = false;
}
this._privateFlags &= ~PFLAG_FORCE_LAYOUT;
}

Expand Down Expand Up @@ -175,7 +189,7 @@ export class View extends ViewCommon implements ViewDefinition {
this._cachedFrame = frame;
let adjustedFrame = null;
let transform = null;
if (this._hasTransfrom) {
if (this._hasTransform) {
// Always set identity transform before setting frame;
transform = nativeView.layer.transform;
nativeView.layer.transform = CATransform3DIdentity;
Expand All @@ -189,7 +203,7 @@ export class View extends ViewCommon implements ViewDefinition {
nativeView.frame = adjustedFrame;
}

if (this._hasTransfrom) {
if (this._hasTransform) {
// re-apply the transform after the frame is adjusted
nativeView.layer.transform = transform;
}
Expand Down Expand Up @@ -363,6 +377,11 @@ export class View extends ViewCommon implements ViewDefinition {
}

public updateNativeTransform() {
if (!this.isLayoutValid) {
this._hasPendingTransform = true;
return;
}

const scaleX = this.scaleX || 1e-6;
const scaleY = this.scaleY || 1e-6;
const perspective = this.perspective || 300;
Expand All @@ -378,12 +397,12 @@ export class View extends ViewCommon implements ViewDefinition {
transform = iOSNativeHelper.applyRotateTransform(transform, this.rotateX, this.rotateY, this.rotate);
transform = CATransform3DScale(transform, scaleX, scaleY, 1);
if (!CATransform3DEqualToTransform(this.nativeViewProtected.layer.transform, transform)) {
const updateSuspended = this._isPresentationLayerUpdateSuspeneded();
const updateSuspended = this._isPresentationLayerUpdateSuspended();
if (!updateSuspended) {
CATransaction.begin();
}
this.nativeViewProtected.layer.transform = transform;
this._hasTransfrom = this.nativeViewProtected && !CATransform3DEqualToTransform(this.nativeViewProtected.transform3D, CATransform3DIdentity);
this._hasTransform = this.nativeViewProtected && !CATransform3DEqualToTransform(this.nativeViewProtected.transform3D, CATransform3DIdentity);
if (!updateSuspended) {
CATransaction.commit();
}
Expand All @@ -409,7 +428,7 @@ export class View extends ViewCommon implements ViewDefinition {
this._suspendCATransaction = false;
}

public _isPresentationLayerUpdateSuspeneded(): boolean {
public _isPresentationLayerUpdateSuspended(): boolean {
return this._suspendCATransaction || this._suspendNativeUpdatesCount > 0;
}

Expand Down Expand Up @@ -689,7 +708,7 @@ export class View extends ViewCommon implements ViewDefinition {
}
[opacityProperty.setNative](value: number) {
const nativeView = this.nativeViewProtected;
const updateSuspended = this._isPresentationLayerUpdateSuspeneded();
const updateSuspended = this._isPresentationLayerUpdateSuspended();
if (!updateSuspended) {
CATransaction.begin();
}
Expand Down Expand Up @@ -845,7 +864,7 @@ export class View extends ViewCommon implements ViewDefinition {
}

_redrawNativeBackground(value: UIColor | Background): void {
const updateSuspended = this._isPresentationLayerUpdateSuspeneded();
const updateSuspended = this._isPresentationLayerUpdateSuspended();
if (!updateSuspended) {
CATransaction.begin();
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/ui/core/view/view-helper/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ class UILayoutViewController extends UIViewController {

IOSHelper.updateAutoAdjustScrollInsets(this, owner);

if (!owner.parent) {
if (!owner.isLoaded && !owner.parent) {
owner.callLoaded();
}
}

public viewDidDisappear(animated: boolean): void {
super.viewDidDisappear(animated);
const owner = this.owner.get();
if (owner && !owner.parent) {
if (owner && owner.isLoaded && !owner.parent) {
owner.callUnloaded();
}
}
Expand Down
0