yahoo / squidb

SquiDB is a SQLite database library for Android and iOS
https://github.com/yahoo/squidb/wiki
Apache License 2.0
1.31k stars 132 forks source link

Proper way to use squidb #260

Closed arashcoder closed 7 years ago

arashcoder commented 7 years ago

Hi. Thanks for this wonderful library. I was wondering what is the proper way of initializing and closing db. Whenever I need to access the database I make new instance of MyDatabase (which inherits from SquidDatabase). Should I use a singleton for the whole application? And I get this warning very often: "A SQLiteConnection object for database 'my-database_db' was leaked." Thanks.

jdkoren commented 7 years ago

Generally speaking, it's better to use a singleton of your subclass of SquidDatabase (or rather, one instance for each individual database file--I'm assuming all your instances refer to the same file, in which case I suggest having just one instance instead.).

sbosley commented 7 years ago

@arashcoder to answer your other question -- if your database is an app-wide singleton (as it should be), you don't need to worry about closing the db. The db will open itself the first time you read from or write your database instance, and it's cheaper to just leave it open at that point. If you make your db instance a singleton and don't bother with closing it, it should eliminate your "connection leaked" warnings.

Also for any future questions, you can join our Gitter chat for support.

arashcoder commented 7 years ago

Thank you for the clarification.