android - closing Cursor in SQLite database explicitly, needed or not needed? -
i understand after database closed, cursor becomes "invalid", close cursor @ same time? avoid having shown below?
example 1
public void string getresultsandreturnstring() { string result = ""; sqlitedatabase db = dbhelper.getreadabledatabase(); cursor cursor = qb.query(db, projection, null, null, null, null, null); cursor.close(); <-- explicit cursor close example 1 db.close(); return result; } example 2
public void cursor getresultsandreturncursor(){ sqlitedatabase db = dbhelper.getreadabledatabase(); cursor cursor = qb.query(db, projection, null, null, null, null, null); return cursor; } public void closeout(cursor cursor, sqlitedatabase database){ cursor.close(); <-- explicit cursor close example 2 database.close(); }
the cursor isn't closed in strict sense closing database (it's still there , can perform operations on it), know, closing database makes cursor useless. should close cursors explicitly after you're done using them number of reasons:
1) noted, after closing database, remaining cursors become "invalid," , cannot relied upon accurate data;
2) see warnings in logcat;
3) risk memory leaks if maintain reference cursor; and
4) it's programming practice close out resources no longer need.
Comments
Post a Comment