Search code examples
javascriptmobilesdktitaniumappcelerator

in which file can I put a script to erase the caches of the Titanium application


I wrote a script that allows you to delete a property in the caches of the application, however I need to run this script only once when I install the application. someone has an idea, thanks

var executed = 0; 
if(executed === 0){
    Ti.App.Properties.removeProperty("My_Property");
    executed++; 
}

Solution

  • The only ways you can hold some value across app sessions are Ti.App.Properties or sql database. So you can do it in two ways as below:

    Solution 1: Use another property to know that you have deleted the desired property.

    // for first time installation, the default value (or 2nd parameter) will be false as you have not deleted the property yet
    var isDeleted = Ti.App.Properties.getBool('My_Property_Deleted', false);
    
    if (isDeleted) {
        Ti.App.Properties.removeProperty("My_Property");
    
        // since you have deleted it, now set it to true so this 'if' block doesn't runs on any next app session
        Ti.App.Properties.setBool('My_Property_Deleted', true);
    
    } else {
        // you have already deleted the property, 'if' block won't run now
    }
    

    Solution 2: Create a new database or pre-load a shipped db with your app.

    // Titanium will create it if it doesn't exists, or return a reference to it if it exists (after first call & after app install)
    var db = Ti.Database.open('your_db');
    
    // create a new table to store properties states
    db.execute('CREATE TABLE IF NOT EXISTS deletedProperties(id INTEGER PRIMARY KEY, property_name TEXT);');
    
    // query the database to know if it contains any row with the desired property name
    var result = db.execute('select * from deletedProperties where name=?', "My_Property");
    
    if (result.rowCount == 0) { // means no property exists with such name
        // first delete the desired property
        Ti.App.Properties.removeProperty("My_Property");
    
        // insert this property name in table so it can be available to let you know you have deleted the desired property
        db.execute('insert into deletedProperties(name) values(?)', "My_Property");
    
    } else {
        // you have already deleted the property, 'if' block won't run now
    }
    
    // never forget to close the database after no use of it
    db.close();
    

    There can be other ways as well, but these 2 will work for what you want. Read more about Ti.Database here