DBFlow icon indicating copy to clipboard operation
DBFlow copied to clipboard

Using DBFlow in more than one module

Open strigare opened this issue 11 years ago • 32 comments

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?

strigare avatar Jun 08 '15 06:06 strigare

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.

agrosner avatar Jun 09 '15 17:06 agrosner

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

eboissonneault avatar Jun 09 '15 18:06 eboissonneault

@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.

strigare avatar Jun 10 '15 04:06 strigare

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

faxinter avatar Jun 30 '15 16:06 faxinter

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")

agrosner avatar Aug 21 '15 01:08 agrosner

Great! Thank you very much.

strigare avatar Aug 21 '15 11:08 strigare

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?

adrianq avatar Oct 26 '15 22:10 adrianq

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.

agrosner avatar Oct 26 '15 22:10 agrosner

@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 avatar Oct 26 '15 22:10 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.

adrianq avatar Oct 27 '15 00:10 adrianq

@agrosner, @adrianq This was a feature that I really need for a project I am working on. As you can see above, I just closed the duplicate issue. I also went ahead and implemented this feature in a forked version of the repo. Here is a link to comparison between my repo and master for DBFlow.

I used the approach discussed on this thread, but made a few changes. In essence, I wanted to be able to specify the module where the databases reside, and use Java-like naming conventions for the DatabaseHolder class. The suffix approach would solve the problem, but would not produce designs with good naming conventions.

The way the approach work is you define a module in the @Database annotation. For example:

@Database(name=ColonyDatabase.NAME, version=ColonyDatabase.VERSION, module=ColonyDatabase.MODULE)
public class ColonyDatabase {
  public static final String NAME = "Colonies";
  public static final int VERSION = 1;
  public static final String MODULE = "Example";  
}

The default module is an empty string. In the code generation, each module is used to generate a DatabaseHolder class. For example, the code above will result in the ExampleGeneratedDatabaseHolder class. Also, this implies that if every database has a module definition, then GeneratedDatabaseHolder class is not generated (i.e., the problem we are currently experiencing should go away).

To load a module that contains a database, you use the new initModule method on FlowManager. Using the example above, one would first initialize FlowManager as we currently do:

FlowManager.init (context);

This will behave as it currently does by attempting to load GeneratedDatabaseHolder. If the holder is not found, then no exception occurs. The module is then be initialized:

FlowManager.initModule ("Example")

It is assumed this call will be handled by the module being imported. This way, the application does not have to load each and every module. The call above will search for the ExampleGeneratedDatabaseHolder class.

The main update with to the FlowManager and DatabaseHolder classes. In the FlowManager class, we have a GlobalDatabaseHolder class that contains a merge of all loaded DatabaseHolder classes. This includes GeneratedDatabaseHolder and the one for each corresponding loaded module. The FlowManager class also contains a map <ModuleName, DatabaseHolder>. I was thinking of making the GlobalDatabaseHolder class contain the mapping, as the merge, but that would require iterating over the maps values to query into each DatabaseHolder object for each operation. That would be too expensive and decided that time is more valuable than space given DBFlow's performance concerns. This, however, can also make it hard use DBFlow if your application+modules have LOTS of databases!

Right now, all unit tests pass!

Please give me your thoughts! I would like to make a pull-request if the design/approach is OK!

hilljh82 avatar Nov 09 '15 02:11 hilljh82

@hilljh82 a very interesting idea and Ill have to check it out. one thing is that 3.0 is very close to being ready for merging into develop and then released on master and bintray or maven central. I would see if there would be any conflicts with this solution on the 3.0 branch. (3.0 is a huge change to library). Or if you would like, I can go ahead, look at your branch and implement it in the same way. If you want credit for it, I would submit a PR against 3.0 and see if your solution works with the code there as is. Thanks for the help, ill have to check this out soon.

agrosner avatar Nov 09 '15 02:11 agrosner

@agrosner I will do some more checks against my target project, then make a PR against 3.0.

hilljh82 avatar Nov 09 '15 02:11 hilljh82

@agrosner I tested my approach in my target project, and it worked. I just had to figure out how to use DBFlow (and its libraries) by inserting them into a lib folder. While testing the approach, I came up with another approach after learning more about android-apt, which I think is better.

What I learned is you can pass arguments to the processor. For example, the following addition to your build.gradle file will pass the target module name to DBFlow-Compiler:

apt {
    arguments {
        targetModuleName 'DBFlow'
    }
}

This will result in the database holder being named DBFlowGeneratedDatabaseHolder. It will be initialized as discussed in previous comments.

I think that this approach would be better than the annotation approach proposed above and currently implemented. Using this new approach, you can really now say that all databases in a project are part of the same module. Using the previous approach, you have to annotate every database. That is somewhat tedious and error prone, and add more complexity to using DBFlow. I have this new approach working, but I have not pushed the changes to my forked version of DBFlow.

Please let me know your thoughts.

hilljh82 avatar Nov 10 '15 11:11 hilljh82

That approach is awesome. yes that would be great. What I want to know is how the FlowManager changes. Maybe you can answer this for me. Do you break the GeneratedDatabaseHolder into multiple ones that the FlowManager references? how does it retrieve the BaseDatabaseDefinition for getDatabaseForTable()? Im sure these questions will get answered in a PR.

agrosner avatar Nov 11 '15 00:11 agrosner

@agrosner. The simple answer is the FlowManager contains its own implementation of a DatabaseHolder (i.e., GlobalDatabaseHolder) that is used to merge the DatabaseHolder implementations for each loaded module, and the GeneratedDatabaseHolder class, if present. The other approach was to do something similar to what you ask above. Have the GlobalDatabaseHolder have a reference to each loaded DatabaseHolder implementation. This approach would require searching through each loaded DatabaseHolder until we found what we were looking for. Although the runtime complexity is the same in both approaches, the latter would require extra function calls. The former uses a batch processing design approach since it only require a single call to get the BaseDatabaseDefinition.

I would like to see how either approach performs. How do you run performance tests?

hilljh82 avatar Nov 11 '15 00:11 hilljh82

After several hours futzing around with DBFlow, I've found this thread that discusses my problem - will this make it into 3.0? Last update was 19 days ago and I'm hoping it will...

AlexCrookes avatar Nov 29 '15 18:11 AlexCrookes

@AlexCrookes. Yes, it should. I have been using the patched version in my projects, and it is working great! I should have time this week to check the patch against the 3.0 branch.

hilljh82 avatar Nov 29 '15 18:11 hilljh82

@hilljh82 that would be great to see a PR on that feature ASAP, so I can finalize the preparations for 3.0. It's shaping up to being a great release!

agrosner avatar Nov 29 '15 18:11 agrosner

@agrosner I have updated the develop branch with the new functionality, and there are no problems right now.

hilljh82 avatar Dec 03 '15 15:12 hilljh82