Kripton ORM has a lot of features and simplifies a lot the developer life. If your application needs to expose its database with a content provider, in most case you have to write it.

I have already written on this feature, so if you didn’t read it, I suggest you read this post too.

Today I take a Room Library example that I found on GitHub named PersistenceContentProviderSample and I rewrite using Kripton. The example is quite simple: there is an application that uses an SQLite database with a table named cheeses and exposes database by a content provider. The developer needed to write:

  • Cheese class definition
  • Cheese DAO interface
  • SampleDatabase
  • SampleContentProvider

Write the content provider is, for me, a very boring task and good bug generator.

Kripton to the same things, without the need to write the content provider. The content provider is generated by Kripton and generation is driven with specific annotations.

To original source code, please have a look directly at the GitHub repository. For simplicity, I write here only the equivalent source code adapted for Kripton. I put “revisited” source code in the repo kripton-examples/PersistenceContentProviderSample.

The DAO definition:

Methods of DAO interface marked with @BindContentProviderEntry, will be used to generate a method that will be used in the content provider. The data source definition:

And at last, the populator task that contains data that populate database first time it is created. The original example has a similar mechanism, but it is included in data source definition.

The generation of the content provider of the data source is pilot by annotation @BindContentProvider that allows defining the authority of content provider, used in the manifest file. The DAO interface used in content provider is marked with @BindContentProviderPath(path = “cheese”). Each method used in content provider is marked with @BindContentProviderEntry. The generated content provider source code is, Javadoc included:

With Kripton, write a content provider is simple as decorated your data source with some annotations.

Some considerations:

  • the main difference between Room Library and Kripton Library in content provider argument is that Kripton generate for us the content provider.
  • The DAO’s methods in Kripton context can be any valid sign, Kripton will generate for us equivalent method that can be used in content provider context.
  • Kripton will generate for use the constants about content provider URI, table names and column names. You can see the Main Activity.

As already stated, you can find the original code on its GitHub repository.

Kripton revisited exampe is kripton-examples/PersistenceContentProviderSample.

For more information about Kripton Persistence Library:

Happy coding!

Francesco

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close