NativeScript / nativescript-background-http

Background Upload plugin for the NativeScript framework
Apache License 2.0
101 stars 50 forks source link

Need Help on task complete handler, UI not updating after variable update inside task event #261

Open RohanPhpDev opened 4 years ago

RohanPhpDev commented 4 years ago

I am using NativeScript version 6.0 and after successful upload of an image using nativescript-background-http i have created a task complete handler inside i call a function to update array which updates UI but UI is not updating and variable show new data in the console. here is sample code: var that=this; context .authorize() .then(function() { return context.present(); }) .then(function(selection) { selection.forEach(function(selected) { var file = selected.android; var name = file.substr(file.lastIndexOf("/") + 1); var fileExt=file.substr(file.lastIndexOf(".") + 1);

          var session = bghttp.session("image-upload");
          var request = {
                  url: *******************",
                  method: "POST",
                  headers: {
                      "Content-Type": "application/octet-stream",
                      "file_name":name,
                  },
                  description: "Uploading " + name,
                  androidNotificationTitle: 'Uploading Image',
                  androidDisplayNotificationProgress:true
              };

          var task = session.uploadFile(file, request); 
          task.on("responded", (e) => {
            let res=JSON.parse(e.data);
            console.log(res.status);
          });
         task.on("complete", (e) => {
            that.fetch_img();
          });
      });
  }).catch(function (e) {
      console.log(e);
});

fetch_img(){ this.gallery.push({"name":"image.jpg"}); console.log(this.gallery); }

I have shown gallery variable in .html file using *ngFor it shows some default image but after task complete, a new object is pushed but it not update UI

PabloPG commented 3 years ago

Try add changeDetectorRef.detectChanges();