在创建新结果之前,我是否必须在Titanium Mobile中关闭结果集,否则一旦没有引用它就会自动“关闭”?
例如,这样的安全和内存泄漏是免费的吗?
var db = db.open("db_name");
var rs = db.execute("SELECT * FROM table");
while(rs.isValidRow()){ /* working with the resuls... */ }
// I make another select before closing the previous (current) results set
rs = db.execute("SELECT * FROM another_table");
while(rs.isValidRow()){ /* working with the results... */ }
// Once I am completely done I close the RS and DB
rs.close();
db.close();
或者每次需要新的选择时我都必须关闭结果集。
var db = db.open("db_name");
var rs = db.execute("SELECT * FROM table");
while(rs.isValidRow()){ /* working with the resuls... */ }
// Close RS and then initialize a new one
rs.close();
rs = db.execute("SELECT * FROM another_table");
while(rs.isValidRow()){ /* working with the resuls... */ }
rs.close();
db.close();
答案 0 :(得分:1)
一旦检索到所有必要的数据,就应该关闭ResultSet。覆盖指向ResultSet的变量不会将其关闭,因此您的第二个示例更合适。
顺便说一下。你宣布两次,所以它应该是:
var db = db.open("...");
var rs = db.execute("SELECT * FROM table");
// while(rs.isValidRow()){ ... }
rs.close();
rs = db.execute("SELECT * FROM another_table"); // Another select
// while(rs.isValidRow()){ ... }
rs.close();
db.close();
甚至可以更好地避免混淆代码中发生的事情:
var db = db.open("...");
var rs1 = db.execute("SELECT * FROM table");
var rs2 = db.execute("SELECT * FROM another_table");
// while(rs1.isValidRow()){ ... }
rs1.close();
// while(rs2.isValidRow()){ ... }
rs2.close();
db.close();
编辑:阅读Best practises for SQLite,详细了解如何在数据库上创建,执行和关闭操作。
答案 1 :(得分:0)
根据文件http://docs.appcelerator.com/platform/latest/#!/api/Titanium.Database.ResultSet
在iOS平台上,关闭数据库也会关闭结果集,也就是说,如果数据库当前处于打开状态,则只能访问结果集
这也可以在iphone/Classes/TiDatabaseProxy.m
(see on Github)
通过查看源代码(同一个文件),我们可以看到TiDatabaseProxy
创建了一个数组,用于存储结果集,然后用于关闭所有结果集。 (see on Github)