Check if table / model exists in local database - isTableExist(STRING tableName)

J Joerg Koch 3 years 6 months ago
1 1 0

Hi all,

the initial startup up my app is pretty slow, since I'm seeding a large products table and that's OK. Yet, on the subsequent starts of the app, the startup should be fast.

I wonder what might be the fastest way to check if a table / model already exists.

I tested:

products = Product.find(:all)
if products.empty?
...

which is of course pretty slow (nearly the same speed as the initial seed...).

product = Product.find(:first)
if product
...

does not accelerate much.

if Product.count == 0
...

is much faster. Still, is there a better way?

I experimented with http://docs.rhomobile.com/en/5.1.1/api/Database#misTableExist:

db = new Rho::Database(Rho::Application.databaseFilePath("local"), "local")
db.isTableExist("Product")

yet, it always returns false and the "new Rho::Database" documentation states "Do not use predefined partition names: app, user, local".

Which is the best way to check if a table exists in the local database?

Thanks in advance for any hints!

Please Register or Login to post a reply

1 Replies

J Joerg Koch

So, I just got on with:

if Product.count == 0 
...
end

If anyone has a hint on how to connect to the local database and use isTableExist(STRING tableName), let me know!

CONTACT
Can’t find what you’re looking for?