我可以在哪个文件中放置脚本来擦除Titanium应用程序的缓存

时间:2017-11-16 22:17:15

标签: javascript mobile sdk titanium appcelerator

我编写了一个脚本,允许您删除应用程序缓存中的属性,但是我需要在安装应用程序时只运行一次此脚本。 有人有个主意,谢谢

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

1 个答案:

答案 0 :(得分:1)

您可以在应用程序会话中保留一些值的唯一方法是Ti.App.Properties或sql数据库。所以你可以通过以下两种方式来做到这一点:

解决方案1:使用其他媒体资源了解您已删除所需的媒体资源。

// 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
}

解决方案2:创建新数据库或使用您的应用预加载已发布的数据库。

// 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();

也可以采用其他方式,但这两种方式可以满足您的需求。的 Read more about Ti.Database here