Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RoomAsset does not seem to load Database [request for help] #3

Closed
giuliofoletto opened this issue Nov 9, 2017 · 9 comments
Closed
Assignees

Comments

@giuliofoletto
Copy link

giuliofoletto commented Nov 9, 2017

Hello,
first let me thank you for creating this package that I find really helpful. However I cannot seem to make it work. It seems to me that the database is not loaded, since any query returns nothing, although the database is populated.
Furthermore, nothing in the console says that the database was loaded, and the same thing happens if I change the name parameter of RoomAsset.databaseBuilder() to a dummy name of a non-existent file. No exceptions are ever thrown and the app does not crash.

TL;DR: regardless of the name of the DB I give to RoomAsset.databaseBuilder(), I get no exceptions but all queries return nothing as if the DB was not populated.

This is my database class file in Java:

package com.mypackage.myapp; 

import android.arch.persistence.db.SupportSQLiteDatabase;
import android.arch.persistence.room.Database;
import android.arch.persistence.room.Room;
import android.arch.persistence.room.RoomDatabase;
import android.arch.persistence.room.migration.Migration;
import android.content.Context;

import com.huma.room_for_asset.RoomAsset;

@Database(entities = {Task.class}, version = 2)
public abstract class AppDatabase extends RoomDatabase {
    private static AppDatabase INSTANCE;
    public abstract TaskDao taskDao(); //TaskDao is my Dao interface
    public static AppDatabase getAppDatabase(Context context) {
        if (INSTANCE == null) {
            INSTANCE =
                    RoomAsset.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "tasksdb.db") .build(); //tasksdb.db is the name of the database file that is in src/main/assets/databases
        }
        return INSTANCE;
    }
    public static void destroyInstance() {
        INSTANCE = null;
    }

This is how the DB is created and queried in an activity

package com.mypackage.myapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

public class DBActivity extends AppCompatActivity {
    AppDatabase mDB;
    private TextView tvContent;
    private Button btnNext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_db);
        mDB = AppDatabase.getAppDatabase(this);
        tvContent=(TextView) findViewById(R.id.tvContent);
        btnNext=(Button)findViewById(R.id.btnNext);
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        final List<Task> testlist = mDB.taskDao().loadAllTasks(); //loadAllTasks is a method defined in my Dao that returns a list of Task, which is my entity class representing one of the tables in the db
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tvContent.setText(String.valueOf(testlist.size()) );
                            }
                        });
                    }
                }).start();
            }
        });

    }
}

In my build.gradle for the app module is:

dependencies {
    //other dependencies unrelated to Room or RoomAsset
    //...
    compile 'android.arch.persistence.room:runtime:1.0.0'
    annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
    implementation 'com.github.humazed:RoomAsset:v1.0'
}

and in my root build.gradle I added maven to the repositories as you explained in the readme.

What is wrong?

@humazed
Copy link
Owner

humazed commented Nov 9, 2017

did you put the DB file in assets/databases?

@giuliofoletto
Copy link
Author

did you put the DB file in assets/databases?

Yes, I should have written it in the text of the issue, but it is the comments to the code. File tasksdb.db is in src/main/assets/databases. However if I use
RoomAsset.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "whatever.db") .build();
and whatever.db is NOT in my assets/databases folder, I get the same results. No exceptions, no crashes, but no query results either, regardless of the existence of the db file.

@humazed
Copy link
Owner

humazed commented Nov 9, 2017

okay, I will take a look and fix the issue, in one hour max I hope.

@giuliofoletto
Copy link
Author

giuliofoletto commented Nov 9, 2017

Thank you, however I am not sure if it is a problem of your code, hence I tagged this question as a [request for help]. Since no support page is provided I had to use github issues. Probably I am doing something wrong, I am wondering if I have missed something in my build.gradle or somewhere.

@humazed
Copy link
Owner

humazed commented Nov 9, 2017

No, in the last version of Room the Android team have introduced some changes which break the lib, but it's easy to fix, so I will fix them first since I don't see you have made a mistake.

@giuliofoletto
Copy link
Author

Ok, thank you again for yor help. When you have finished let me know which version to use in my

implementation 'com.github.humazed:RoomAsset:v1.0'

line of the build.gradle file, or post the version number in the github release page.

@humazed humazed self-assigned this Nov 9, 2017
@humazed
Copy link
Owner

humazed commented Nov 9, 2017

I was mistaken the new changes didn't affect the database but only the sample project so I updated the sample.
if you use a non-existing database file name you should get the exception:
com.readystatesoftware.sqliteasset.SQLiteAssetHelper$SQLiteAssetException: Missing databases/tasksdb.db file (or .zip, .gz archive) in assets, or target folder not writable

I have tried your code and it's working for me.

my guess is that you used empty DB first then tried to use wrong names. but didn't change the version so RoomAsset didn't look at the asset folder hence no exception is thrown.

so I need you to uninstall your app and try again.

@giuliofoletto
Copy link
Author

That is great, my mistake! It now works. So RoomAsset only looks in the assets folder the first time the app is run, correct? Is there any way to force it to look again? Should I increase the version number and add a migration?
Thank you for your help.

@humazed
Copy link
Owner

humazed commented Nov 9, 2017

yes, it will be wasteful to look every time the app starts, so the only way is to increase the version and to add a migration.

@humazed humazed closed this as completed Nov 9, 2017
@humazed humazed reopened this Nov 9, 2017
@humazed humazed closed this as completed Nov 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants