Office Hours — Today, June 1

Yesterday, May 31

Jun 1
7:25 PM
Mark M.
has entered the room
Mark M.
turned on guest access
chike m.
has entered the room
Mark M.
hello, chike!
how can I help you today?
chike m.
Hi Mark
ok, let me paste it
7:30 PM
chike m.
View paste (25 more lines)
I have 3 fragments in a viewpager. Each of these fragments performs a 
db fetch operation. for example fragment A will perform:
getUsers() {
	SqliteDatabase db = mDbHelper.getReadableDatabase();
	
	// do operation

	db.close();
}

Fragment B will be 

getPosts() {
	SqliteDatabase db = mDebHelper.getReadableDatabase();
	
...
Mark M.
first, get rid of the close() calls
and I mean *all* the close() calls
chike m.
ok
Mark M.
as you would appear to be closing the database in many places, including the one triggering "The connection pool for /data/user/0/com.chikeandroid.debtmanager20.mock/databases/Debts.db has been closed but there are still 1 connections in use"
chike m.
but it can lead to leaking of the db
Mark M.
not if it is a singleton
by definition, it was leaked when you wrote your app
chike m.
ok
Mark M.
a database is one of those things that typically we can never close, as we never know when it is safe to do so
so, you just leave it open
it will remain open as long as the process does
(or, rather, as long as the process is alive)
chike m.
So since it's a singleton, I should not close it
Mark M.
and there is no particular harm in the process being terminated with an open database
correct
now, in some of my book samples, I close the database
but that is because it is only being referenced in one class, typically a fragment
and I know when I am done with it (i.e., when the fragment is destroyed)
chike m.
alright, I get it now
Mark M.
but, that's only typically practical for tiny projects
chike m.
Alright, Mark! thanks
Mark M.
I am not sure if you are familiar with "the seven stages of grief", but I too wanted to always close the database, and I went through those seven stages and am now at "acceptance" that we just don't need to do it
"-)
er, :-)
(gosh, I cannot even type an emoticon today...)
7:35 PM
chike m.
haha
:-)
one more thing
If I am using Loaders in a fragment and also using setRetainInstance(true), just for retaining the data.
are they not performing the same thing somehow?
Mark M.
yes
a Loader will retain whatever it loaded (e.g., a Cursor)
if the fragment gets recreated due to a configuration change, you will be given the loaded data again in the new fragment via the appropriate callback
I do not think that I have tried using a Loader from a retained fragment, so I do not know if those will conflict
chike m.
ok
I think I'm done for now!
Mark M.
OK
chike m.
Thanks Mark!
Mark M.
you're welcome!
chike m.
has left the room
8:25 PM
Mark M.
turned off guest access

Yesterday, May 31

 

Office Hours

People in this transcript

  • chike mgbemena
  • Mark Murphy