23.02.2011 14:14, Indicator Veritatis пишет:
Well, yes, one could do that, but it seems redundant, since
SQLiteOpenHelper is already providing a singleton access to the
database.

SQLiteOpenHelper is not a singleton:

- One can create as many instances of SQLiteOpenHelper (subclass, actually) as one would like.

- The database object instance inside is reference counted (provided by SQLiteClosable base class), but reference counting is very much a different concept from a singleton.

- No effort is made by SQLiteOpenHelper or its subclasses to ensure that there is only one database object instance (for a given SQLiteOpenHelper subclass), no matter how many "wrapper" helpers exist. I don't believe that it should, but that would qualify as singleton-type semantics - however, it's not there.

I can think of two ways to access a database from multiple activities (that's not counting the best in my opinion - a private ContentProvider):

- Have each activity open and close the database independently of others. This requires care, so as to not leak database objects (which have a native part, and an open file, so these leaks can be particularly unpleasant). It can also be somewhat inefficient, as each activity would need to open and close its database connection multiple times.

- Ensure there is only one open database object, shared by all components of the application.

The second way can be implemented by wrapping SQLiteOpenHelper in a simple Java singleton. This guarantees that there is only once instance of the database (efficiency), and it's always open when needed (correctness).

And I wouldn't call a dozen or so lines of code that guarantee efficiency and correctness for database access in the entire application "redundant".

-- Kostya

All your proposal adds is singleton access to its Context on
top of that. In fact, it might actually make the code more readable if
ydm adds his own singleton for only that Context, and then always does
the getInstance() on that context in the call to whatever class he
defines as extending SQLiteOpenHelper.

At least that way the reader will not be misled by the redundancy.

On Feb 22, 2:46 am, Kostya Vasilyev<kmans...@gmail.com>  wrote:
22.02.2011 13:32, ydm пишет:

I'm curious why the SQLite db requires a Context object,
The database class doesn't - SQLiteOpenHelper (subclass) does, to get
the location of the database file.

and what I
should do to share the same instance of a db object between many
activities? Should I initialize it in the first activity and use it
across the application, or may be any activity should reinitialize the
db with itself as db context?
If you're not going to implement a private content provider (which is
one way), I'd use a simple singleton, keeping a reference to one and
only SQLiteOpenHelper subclass object, initialized with the application
context.

-- Kostya

--
Kostya Vasilyev --http://kmansoft.wordpress.com


--
Kostya Vasilyev -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to