Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor
Accepted Solution

Download file in a thread

After a bit of a struggle and with help from an Advisor I got code working for download of a zip file. On Ripple the download takes around 14 seconds. I would guess it takes longer on a device.

So I definitely want to do this in seprate thred in order to not locking up the GUI.  The code I have makes us of DOM object so I cannot use a thread with a "Worker". DOM is not  thread safe.

 

Any suggestion anyone?

 

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.
Developer
interfaSys
Posts: 817
Registered: ‎11-19-2009
My Device: Z10, Q10, 9900, 9790, PlayBook,
My Carrier: T-Mobile UK, Three, O2, Orange, Sunrise, Swisscom

Re: Download file in a thread

[ Edited ]

If you're using blackberry.io.filetransfer.download, it's async, so shouldn't lock the GUI.

And if you're using something else, wouldn't it be possible to only keep the non-GUI code in the worker?

--
Olivier - interfaSys ltd
Developing for BlackBerry 10 devices using the Sencha Touch framework.
Please use plain text.
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor

Re: Download file in a thread

[ Edited ]

I have just finished code using XMLHttpRequest using asynhcronous. it works fine got a blob with the zip file.

so the next step is unzipping it. Have cloned github for the unzip api. would be nice to be able to do it directly on the blob but I guess I have to do it on the file system. Writing to the file system seems to be connected to the DOM so I cannot do that in a  thread  it seems. The DOM is not thread safe I have learned.

 

Now its sleeping time (GMT)

 

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.
Developer
interfaSys
Posts: 817
Registered: ‎11-19-2009
My Device: Z10, Q10, 9900, 9790, PlayBook,
My Carrier: T-Mobile UK, Three, O2, Orange, Sunrise, Swisscom

Re: Download file in a thread

Oh, so that's the unzipping part which requires the DOM... :smileysad:, but at that stage, wouldn't it be best to just unzip the file in the app and display a spinning wheel? It's the downloading part which takes 14s, correct?

And yes, workers cannot use the DOM.

--
Olivier - interfaSys ltd
Developing for BlackBerry 10 devices using the Sencha Touch framework.
Please use plain text.
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor

Re: Download file in a thread

Correct, its the download that takes 14 seconds.

 

I am now working with the part of getting the blob to a file . Most of the code dealing with this uses nested function calls passing functions. Not familiar with that method. I can see it makes for compact code but readability.  Not to me.

And I hate just copying code and use it. In my old days at IBM we called that  "parrot programming"

 

So even if it results in more code writing I prefer to break it up in parts for better "visibility".  Its like with cars. I prefer an old fashion reliable chevy 350  instead of supercharged modern Mercedes engine  :-)

 

And yes, the unzip could be made in the app. It is a bit clumsy that you need the file system for unzipping

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.
Developer
interfaSys
Posts: 817
Registered: ‎11-19-2009
My Device: Z10, Q10, 9900, 9790, PlayBook,
My Carrier: T-Mobile UK, Three, O2, Orange, Sunrise, Swisscom

Re: Download file in a thread

I agree on all the inline callback functions. That's the reason I requested sync functions to be added to the API :smileywink:

--
Olivier - interfaSys ltd
Developing for BlackBerry 10 devices using the Sencha Touch framework.
Please use plain text.
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor

Re: Download file in a thread

I have taken out the inline functions on the first level to onSuccess and onError and that makes it more readable.

 

from:

http://klanguedoc.hubpages.com/hub/How-To-Use-HTML5-File-System-Access-API

 

Got a bit stuck on the next level on "taking apart"  "onSuccess"

 

if('webkitRequestFileSystem' in window)
 {
   window.requestFileSystem = window.webkitRequestFileSystem;
   window.requestFileSystem(window.PERSISTENT, 10*1024*1024, onSuccess, onError);
 }else
    if('moz_requestFileSystem' in window)
     {
      //firefox
      window.requestFileSystem = window.moz_requestFileSystem;
      window.requestFileSystem(window.PERSISTENT, 10*1024*1024, onSuccess, onError);
     }
 {
    alert("unsupported browsr");
 }

function onError(e)
 {
  alert("Getting filesystem failed " + e);
 }

 
function onSuccess(fs)
{
    fs.root.getFile('storage.txt', {create:true}, function(fileEntry){
        fileEntry.createWriter (function(f)
            {
                f.onwriteend = function(event) {};
                f.onerror = function(err) {};
                BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;
                var writer = new BlobBuilder();
                writer.append('Abrakadabra');
                f.write(writer.getBlob('text/plain'));
            }
        )
    }
 
}

 

 

 

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor

Re: Download file in a thread

Solution.  Use XMLHttpRequest with param Asynchronous  "true"

set to blob and write to file system. The download goes asynchronous and file writing synchronous

 

Code below is a bit messy with debugging and also the failing unzip code but I think the general idea is pretty clear

There is a warning for "arraybuffer" telling to use "ArrayBufferView" instead but "ArrayBufferView" turns up as not supported

 

 

 var myblob;
 
function DL5(myURL)
 {
 
  xhr = new XMLHttpRequest();
  xhr.open('GET', myURL, true);  //Asynchronous.
  //xhr.responseType = 'blob';
  xhr.responseType = 'arraybuffer';
 //  xhr.responseType = 'ArrayBufferView';         //Not supported???
  // var builder = new WebKitBlobBuilder();
  xhr.onload = function (e)
  {
     
  //   builder.append(xhr.response);
  //     var blob = builder.getBlob("config.zip");
    
     myblob = new Blob([xhr.response], {type: "zip"});

   if (onSuccess)
     {
       onSuccess("Download OK!!");
     }
  };
xhr.send();
}

function onSuccess(mess)
 {
   alert (mess);
   DLX();
 }

function DLX()
 {
   requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;  
   blackberry.io.sandbox = false;
   requestFileSystem(window.PERSISTENT, 10*1024*1024, onSuccess2, onError2);
 }
function onError2(e)
 {
  alert("Getting filesystem failed " + e);
 }

function onSuccess2(fs)
{  
    alert("into Success2");
    fs.root.getFile(blackberry.io.home + "/config.zip", {create: true}, function (fileEntry) {
        fileEntry.createWriter(function(f)
            {
                f.onwriteend = function(event) {
                alert("write end");
                
                var txt = community.unzip.unzipPackage(blackberry.io.home + "/config.zip", blackberry.io.home/+ "/unzip");
                var JSONobject = eval(txt);
                
                alert("done unzip");
                };
                f.onerror = function(err) {
                alert("write error");
                };

                f.write(myblob);
                alert("File saved!");
            }
        )
     }
    )
}

 

 

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.