You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How could nativescript yield the execution to let UI redraw. #2382
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"
});
}
});
};
@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.
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.
@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.
Activity
NathanaelA commentedon Jun 29, 2016
ryc16 commentedon Jun 29, 2016
@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.
Then I just add below code to "ontap" event.
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.
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.
NathanaelA commentedon Jun 29, 2016
ryc16 commentedon Jul 1, 2016
@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.
lock commentedon Aug 29, 2019
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.