Project: titanium
Issue TIMOB-17502: iOS: URLSession Module returns blob instead of file location - causing low memory crash
h4. Problem Description downloadcompleted returns e.data as a blob in physical memory!! If the file size is large, for example, over 1GB, it could cause low memory crash. Plus, using file.write(e.data) in this case takes too much time to run in the background. iOS only allows 30 seconds. Chances are the app will be either crashed or suspended before the file is saved. I suggest: upon downloadcompleted move the file to a temp directory and return the path instead of a blob. h4. TESTING ENVIRONMENT CLI version 3.3.0, Titanium SDK version 3.3.0.GA iOS7 h4. CODE SAMPLE h3. app.js {code} // Require in the urlSession module var urlSession = require("com.appcelerator.urlSession"); var session; // App UI var win = Ti.UI.createWindow({backgroundColor:"white"}); var progress = Ti.UI.createProgressBar({ width: 200, height: 50, min: 0, max: 1, value: 0, style: Ti.UI.iPhone.ProgressBarStyle.PLAIN, top: 10, message: 'Downloading image URL', font: { fontSize: 12, fontWeight: 'bold'}, color: '#888' }); win.add(progress); var imageView = Ti.UI.createImageView({ top:150, height:300, width:200 }); win.add(imageView); var button = Ti.UI.createButton({ title:'Download Image (url)', height:40, width:200, top:70 }); button.addEventListener('click', function() { // Create a session configuration // The string parameter is an arbitrary string used to identify the session in events var sessionConfig = urlSession.createURLSessionBackgroundConfiguration("com.appcelerator.test"); // Create a session session = urlSession.createURLSession(sessionConfig); // Create a background download task to get the asset with the URL urlSession.backgroundDownloadTaskWithURL(session,"https://raw.github.com/appcelerator-developer-relations/KitchenSink/master/Resources/images/dog@2x~iphone.jpg"); progress.show(); }); win.add(button); win.open(); // Monitor this event to receive updates on the progress of the download Ti.App.iOS.addEventListener("downloadprogress", function(e) { // Update the progress indicator progress.value = (e.totalBytesWritten/e.totalBytesExpectedToWrite); }); // Monitor this event to know when the download completes Ti.App.iOS.addEventListener("downloadcompleted", function(e) { Ti.API.info("download completed " + JSON.stringify(e)); // Update the image imageView.image = e.data; // Invalidate the session and cancel current session tasks urlSession.invalidateAndCancel(session); // Notify the user the download is complete if the application is in the background Ti.App.iOS.scheduleLocalNotification({ alertBody:"Download complete!", date:new Date().getTime() }); progress.hide(); }); // Monitor this event to know when all session tasks have completed Ti.App.iOS.addEventListener('sessioncompleted', function(e) { Ti.API.info("sessioncompleted " + JSON.stringify(e)); if (e.success) { alert("Downloads completed successfully."); } }); {code} h4. STEP TO REPRODUCE - Create a new Project - Double-click the Project's "tiapp.xml" file to open it in the Overview tab. - In the Modules section, click the Add button (green plus sign) to open the Mobile Modules dialog. - Locate and select "com.appcelerator.urlSession". This module is part of the Titanium SDK since Release 3.2.0 - Click OK. - Copy the code segment above to the project "app.js" file - Run the Project h4. OBSERVED RESULT If the URL is changed to download a large file, progress bar would not response and the app will crash after some time.
Story points: 8