Skip to content

Using DBFlow in more than one module #266

Closed
@strigare

Description

@strigare

When I try to use DBFlow in more than one module I get an error. I figured out it was because it creates two DataBaseHolder classes in the same package. Is there a way to avoid this and use only one class for the whole project?

Activity

agrosner

agrosner commented on Jun 9, 2015

@agrosner
Owner

Not currently possible with annotation processing. Not sure if there are techniques or ways around this, but my understanding of annotation processing is that they run in isolation from each other. Meaning, the annotation processor in one module has no knowledge of other annotations from another project.

eboissonneault

eboissonneault commented on Jun 9, 2015

@eboissonneault

Could you simply add a prefix for GeneratedDatabaseHolder in order to avoid duplicates or would this cause other issues?

strigare

strigare commented on Jun 10, 2015

@strigare
Author

@eboissonneault I was thinking something along the same line. I'm probably going to try and give it a go this week.

@agrosner the problem is not that I need the annotation processing to know of the existence of all the modules in order for them to interact, the problem is that when the code gets compiled into the dex files it throws an error because it finds two classes that share the same fully qualified name (DataBaseGenerator in the same package). This is not really a problem until I want to develop a library and the project that includes it is also trying to use DBFlow. Of course I could be mistaken, I'm just now starting to use both gradle and this library. If you could give me instructions on how to build it (in order to include it as a maven repo and test changes) I would really appreciate it.

faxinter

faxinter commented on Jun 30, 2015

@faxinter

i have the same problem,use DBFlow in more than one module

self-assigned this
on Aug 21, 2015
added this to the 3.0.0 milestone on Aug 21, 2015
agrosner

agrosner commented on Aug 21, 2015

@agrosner
Owner

alright. Ill add the ability to do so in 3.0.0, which lets you change the generated class file name. But youll have to do something like this: FlowManager.init(context, "_MyModule"); which will append it to the end of the file name and in your Database:

@Database(...holderClassSuffix="_MyModule")
strigare

strigare commented on Aug 21, 2015

@strigare
Author

Great! Thank you very much.

adrianq

adrianq commented on Oct 26, 2015

@adrianq

Hi @agrosner! Finally did you manage to get this into 3.0.0? I was having a look at the code and it doesn't look like. I have tried to implement it in a fork. I have been able to generate the classes using the suffix but I am stuck in something probably quite stupid. When I executed FlowManager.init(context, "_MyModule"); the second time (I have to execute it twice: the first one for the module, the second one for the main app), mDatabaseHolder is not null. Therefore getDatabaseHolder() returns the first holder and never instantiate the second GeneratedDatabaseHolder (I am talking about this method). Can you shed some light on this? What am I missing?

agrosner

agrosner commented on Oct 26, 2015

@agrosner
Owner

hey @adrianq not quite yet, I will need to add it at some point. But if you get it working, make the PR against the 3.0 branch, that would be great. So what's missing is the ability for you to define the GeneratedPlaceHolder class that DBFlow looks for. The generated class is always in same package and may get overwritten (as you mention here). What's missing is the ability to define where this placeholder class lives, so we could essentially operate with two different ones for each module. What we would need is to add a simple FlowManager.init(context, "_MyModule"); method to that class and have it look for a class with that postfix in the FlowManager instead of always checking for :"

 protected static DatabaseHolder getDatabaseHolder() {
        if (mDatabaseHolder == null) {
            try {
                mDatabaseHolder = (DatabaseHolder) Class.forName(
                        "com.raizlabs.android.dbflow.config.GeneratedDatabaseHolder").newInstance();
            } catch (Throwable e) {
                throw new RuntimeException(e);
            }
        }

        return mDatabaseHolder;
    }

It would append or change that class that was created. The only downside is that you'd need to specify which holder to put the @Database into.

adrianq

adrianq commented on Oct 26, 2015

@adrianq

@agrosner and we kind of merge GeneratedDatabaseHolder and GeneratedDatabaseHolder_suffix into the same mDatabaseHolder? or are we using two different mDatabaseHolder? I am not sure if I understood you right. I already generate the two GeneratedDatabaseHolder but the two apps (module and main app) are sharing the same FlowManager. So mDatabaseHolder != null by the time the second FlowManager.init(context, "_MyModule"); is called.

adrianq

adrianq commented on Oct 27, 2015

@adrianq

@agrosner I've made it work merging the two GeneratedDatabaseHolder into a single mDatabaseHolder. It is looking good so far although I will have to test it a little bit more. If you are happy with this approach, I can clean up the code and create a PR.

24 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @strigare@FarhanAhmad@AlexCrookes@skhanzada@hilljh82

      Issue actions

        Using DBFlow in more than one module · Issue #266 · agrosner/DBFlow