Skip to content

How could nativescript yield the execution to let UI redraw.  #2382

Closed

Description

@ryc16

I read some discussion where Nativescript not yet have background worker and everything is running UI thread. Is there any way to yield the execution to let UI redraw.

Bolow is a good example from one of plugins people implemented to read the phone contacts. If there is 1000 contacts, the UI is totally blocked for few mins. Any idea to yield the execution.

exports.getAllContacts = function(){
    return new Promise(function (resolve, reject){
        var c = appModule.android.context.getContentResolver().query(android.provider.ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

        if(c.getCount() > 0){
            var cts = [];
            while(c.moveToNext()){
                var contactModel = new Contact();
                contactModel.initializeFromNative(c);
                cts.push(contactModel);
            }
            c.close();
            resolve({
                data: cts,
                response: "fetch"
            });
        }
        else{
            c.close();
            resolve({
                data: null,
                response: "fetch"
            });
        }
    });
};

Activity

  1. NathanaelA commented on Jun 29, 2016

    @NathanaelA
  2. ryc16 commented on Jun 29, 2016

    @ryc16
    Author

    @NathanaelA, thanks for your advice. I am not sure the difference on option #1 and #2. I thought SetImmediate is part of your plugin. I tried to understand thme more but couldn't get more information. From what I research so far, I believe your plugin's SetImmediate is using node's setTimout. I tried to do a demo to prove of concept by creating the project.

    tns create demo
    tns plugin add nativescript-master-technology
    

    Then I just add below code to "ontap" event.

      for (var i = 0; i < 2000000000; i++) {
                if (i === 0) {
                    this.counter--;
                    this.set("message", getMessage(this.counter));
                }
    
                if (i === 200) {
                    setImmediate(function () {
                        console.log("setImmediate");
                    });
                }
            }
            console.log("DONE");
    

    The counter got redraw on UI when the loop is complete ONLY (around 7 seconds). So the execution still not yield UI to redraw. So I am not sure if I use your plugin correctly.

    I used setTimeout to break up the code but faced different problem. ie put setTimeout inside while loop as shown below but the cursor "c" got close before running contactModel.initializeFromNative(c). If I move the c.close(); inside of last loop but I got other error.

            while (c.moveToNext()) {
                setTimeout(function () {
                    var contactModel = new Contact();
                    contactModel.initializeFromNative(c);
                    cts.push(contactModel);
                }, 100);
            }
    

    Appreciate If you have sample to demo how your plugin work. Please hack above exports.getAllContacts() function directly and show the solution here. If there is no solution, then have to go for option #3 but try not to because not familiar with native java/obj c.

  3. NathanaelA commented on Jun 29, 2016

    @NathanaelA
  4. ryc16 commented on Jul 1, 2016

    @ryc16
    Author

    @NathanaelA, Got you and works. Thanks for the information. By the way, even it works, running task in UI thread really slow down a lot of UI. The UX experience is really bad. From above example, reading 500+ contacts from device takes very long time. Hope there is solution (like background thread) very soon. Not sure if there is any way to improve the UX experience. Maybe have to move this task to native side.

  5. lock commented on Aug 29, 2019

    @lock

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  6. locked and limited conversation to collaborators on Aug 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions