Nativescript: App crashed if do app.run() in async callback

Created on 27 Jan 2019  路  4Comments  路  Source: NativeScript/NativeScript

Environment

  • CLI: 5.1.1
  • Cross-platform modules: 5.1.2
  • Android Runtime: 5.1.0
  • iOS Runtime:
  • Plugin(s):

I need this, for making some async initialization before app.run()

To Reproduce

Keep screen turned ON during app.run() (if during app.run() screen turned OFF all works fine)

import * as app from "tns-core-modules/application";
import {parse} from "tns-core-modules/ui/builder";

setTimeout(() => {
    app.run({
        create: () => parse(`<StackLayout><Label text="Some text"/></StackLayout>`)
    });
}, 10);
System.err: java.lang.RuntimeException: Unable to start activity ComponentInfo{org.nativescript.viewasstringissue/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: 
System.err: Calling js method onCreate failed
System.err: 
System.err: TypeError: Cannot read property 'create' of undefined
System.err: File: "file:///data/data/org.nativescript.viewasstringissue/files/app/tns_modules/tns-core-modules/ui/builder/builder.js, line: 75, column: 14
System.err: 
System.err: StackTrace: 
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.viewasstringissue/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 75, column: 15
System.err:     Frame: function:'FrameBase.navigate', file:'file:///data/data/org.nativescript.viewasstringissue/files/app/tns_modules/tns-core-modules/ui/frame/frame-common.js', line: 119, column: 30
System.err:     Frame: function:'ActivityCallbacksImplementation.setActivityContent', file:'file:///data/data/org.nativescript.viewasstringissue/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 904, column: 30
System.err:     Frame: function:'ActivityCallbacksImplementation.onCreate', file:'file:///data/data/org.nativescript.viewasstringissue/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 763, column: 14
System.err:     Frame: function:'NativeScriptActivity.onCreate', file:'file:///data/data/org.nativescript.viewasstringissue/files/app/tns_modules/tns-core-modules/ui/frame/activity.js', line: 20, column: 25
System.err: 
System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
System.err:     at android.app.ActivityThread.-wrap14(ActivityThread.java)
System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
System.err:     at android.os.Looper.loop(Looper.java:154)
System.err:     at android.app.ActivityThread.main(ActivityThread.java:6776)
System.err:     at java.lang.reflect.Method.invoke(Native Method)
System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
System.err: Caused by: com.tns.NativeScriptException: 
System.err: Calling js method onCreate failed
System.err: 
System.err: TypeError: Cannot read property 'create' of undefined
System.err: File: "file:///data/data/org.nativescript.viewasstringissue/files/app/tns_modules/tns-core-modules/ui/builder/builder.js, line: 75, column: 14
System.err: 
System.err: StackTrace: 
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.viewasstringissue/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 75, column: 15
System.err:     Frame: function:'FrameBase.navigate', file:'file:///data/data/org.nativescript.viewasstringissue/files/app/tns_modules/tns-core-modules/ui/frame/frame-common.js', line: 119, column: 30
System.err:     Frame: function:'ActivityCallbacksImplementation.setActivityContent', file:'file:///data/data/org.nativescript.viewasstringissue/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 904, column: 30
System.err:     Frame: function:'ActivityCallbacksImplementation.onCreate', file:'file:///data/data/org.nativescript.viewasstringissue/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 763, column: 14
System.err:     Frame: function:'NativeScriptActivity.onCreate', file:'file:///data/data/org.nativescript.viewasstringissue/files/app/tns_modules/tns-core-modules/ui/frame/activity.js', line: 20, column: 25
System.err: 
System.err:     at com.tns.Runtime.callJSMethodNative(Native Method)
System.err:     at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1120)
System.err:     at com.tns.Runtime.callJSMethodImpl(Runtime.java:1000)
System.err:     at com.tns.Runtime.callJSMethod(Runtime.java:987)
System.err:     at com.tns.Runtime.callJSMethod(Runtime.java:967)
System.err:     at com.tns.Runtime.callJSMethod(Runtime.java:959)
System.err:     at com.tns.NativeScriptActivity.onCreate(NativeScriptActivity.java:18)
System.err:     at android.app.Activity.performCreate(Activity.java:6956)
System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
System.err:     ... 9 more


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Most helpful comment

@NickIliev in my case it's not HTTP request, but interaction with database.
But anyway, thanks for the recommendations.
But I still think it would be useful to provide ability of asynchronous initialization in the framework. And while it goes, to keep showing splash screen.
For example, entry point could be Promise indicating that initialization is done, than we could manually hide splash screen and do app.run() or make additional initialization and only after that hide splash screen. This will add flexibility and additional control on the application.

All 4 comments

@webleaf perhaps you could do the async initializations in the landing page and not in the entry file?

@NickIliev my landing page can change on app.run() (what page will be opened depends on what create method of app.run() NavigationEntry return).
I have two ways:

  • await for initialization Promise throughout all application, where I want use method, which depends on it;
  • or just run initialization before app.run() and no longer think about it.

Second way looks more clear for me.

@webleaf if your async operations are somehow slow and time-consuming that would effectively cause the app to load nothing until the operation is done. Let's assume that the async call is depending on some HTTP request.. Do we really have a scenario where we would want the users to look at a blank white screen while waiting for the async operation to complete (e.g. with slow internet connection)? In my opinion, you should always have a fast loading landing page and change the UI or navigate to subpage when the async call is done. Looking at a blank white screen is bad UX in my understanding.

With NativeScript 5.x.x you can have root Frame which navigates to a default page. So I would think that you could still change the landing page but not in the entry application file but in the application root.

For NativeScript Core we have this structure with app-root which loads the landing page. So you could create your async calls and change the landing page by navigating in the root frame (e.g by getting the frame reference with getFrameById or topmost methods).

@NickIliev in my case it's not HTTP request, but interaction with database.
But anyway, thanks for the recommendations.
But I still think it would be useful to provide ability of asynchronous initialization in the framework. And while it goes, to keep showing splash screen.
For example, entry point could be Promise indicating that initialization is done, than we could manually hide splash screen and do app.run() or make additional initialization and only after that hide splash screen. This will add flexibility and additional control on the application.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rLoka picture rLoka  路  3Comments

Leo-lay picture Leo-lay  路  3Comments

fmmsilva picture fmmsilva  路  3Comments

kn9ts picture kn9ts  路  3Comments

Pourya8366 picture Pourya8366  路  3Comments