Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Function crashing on some Android devices, memory issue?

$
0
0

Hi folks,

Need some help optimising this function below for Android.

I have an app which does some pre-loading and fetches a load of markers to use in several map views later on in the app.

Once the load is finished, the main views are then loaded in.

On the clients Android device (Xperia Z1) this crashes and they can never open the app. It works on pretty much every other device and simulator. But I can see a grow heap being logged in the log.

Can anyone suggest a way of improving the below to stop this happening?

This function sends the URL and FILENAME to the receiving function

function getMapMarkers() {
    // get the species list back
    var db = Ti.Database.open('myDB');
    var getSpeciesImages = db.execute('SELECT speciesiconfilename, speciesmapiconurl FROM species where speciesiconfilename <> ""');
 
    // YOU ONLY NEED TO DO THIS ONCE SO DO IT OUTSIDE THE LOOP
    var imgDir = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'map_marker_icons');
 
    // if the directory doesn't exist, then we need to create it
    if (!imgDir.exists()) {
        // If the directory doesn't exist, make it
        imgDir.createDirectory();
    };
 
    // start the loop
    while (getSpeciesImages.isValidRow()) {
        var filename = getSpeciesImages.fieldByName('speciesiconfilename');
        var url = getSpeciesImages.fieldByName('speciesmapiconurl');
 
        getMarker(url, filename);
 
        getSpeciesImages.next();
 
    } // end the loop
 
    getSpeciesImages.close();
 
 
    // close the database
    db.close();
 
    // get the exhibit markers next
    getExhibitMapMarkers();
};
This one does the downloading of the marker, resizing and saving to disk
function getMarker(url, filename) {
 
    var mapMarker = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'map_marker_icons', filename);
 
    // now we need to download the map marker and save it into our device 
 
    var getMarker = Titanium.Network.createHTTPClient({
        timeout: 30000
    });
 
    getMarker.onload = function() {
 
        // if the file loads, then write to the filesystem
        if (getMarker.status == 200) {
 
                var image = this.responseData;
                var resizedImage = image.imageAsResized(75, 75);
 
                // download first and save to disk without resize
                //mapMarker.write(this.responseData);
                mapMarker.write(resizedImage);
 
                image = null;
                resizedImage = null;
 
 
            //I ALWAYS NULL ANY PROXIES CREATED SO THAT IT CAN BE RELEASED
            mapMarker = null;
        } else {
            Ti.API.info("Image not loaded");
        }
 
 
        getMarker = null;
 
        // load the tours in next
        loadNav();
 
 
 
    };
 
    getMarker.onerror = function(e) {
        Ti.API.info('XHR Error ' + e.error);
        //alert('markers data error');
    };
 
    getMarker.ondatastream = function(e) {
        //Ti.API.info('Download progress: ' + e.progress);
    };
 
    // open the client
    getMarker.open('GET', url);
 
    // change the loading message
    MainActInd.message = 'Downloading Markers';
    // show the indicator
    MainActInd.show();
 
    // send the data
    getMarker.send();
 
}
Many thanks!

Simon


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>