How to Use Freezer: A Simple Fluent Android ORM

Oct 1, 2023 | Programming

Welcome to your journey with Freezer, a streamlined Android ORM that’s not just easy to use but compatible with RxJava2. With Freezer, managing your data becomes smoother than ever—let’s guide you through the essentials!

Getting Started with Freezer

Step 1: Initialize Freezer

Before you can do anything fun, you need to initialize Freezer in your application. Here’s how to do it:

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Freezer.onCreate(this);
    }
}

Step 2: Annotate Your Models

Now that you’ve initialized, let’s make sure your data models are properly annotated to be persisted:

@Model
public class User {
    int age;
    String name;
    Cat cat;
    List pets;
}

@Model
public class Cat {
    @Id long id;
    String name;
}

Step 3: Use Entity Managers

It’s time to make your managers work for you! Here’s what you can do:

  • Persist Data: Create and add new objects easily:
  • UserEntityManager userEntityManager = new UserEntityManager();
    User user = ...; // Create a new object
    userEntityManager.add(user);
  • Querying: Use Freezer’s fluent interface to construct complex queries:
  • List allUsers = userEntityManager.select()
                                          .name().startsWith("Flo")
                                          .asList();

Adding Some Fun: Complex Queries

Let’s say you’re flipping through a book to locate a specific chapter. Freezer helps you search for your data similarly. Here’s how:

List allUsers = userEntityManager.select()
                                      .name().equalsTo("Florent")
                                      .or()
                                      .cat(CatEntityManager.where().name().equalsTo("Java"))
                                      .asList();

Selectors and Aggregation

You have various selectors for filtering users by age, name, or even date. Here are a few examples:

userEntityManager.select()
    .age().greaterThan(5)
    .name().contains("flo")
    .asList();

Aggregation is a cakewalk; you can find the sum, average, minimum, and maximum ages directly:

float agesAverage = userEntityManager.select().average(UserColumns.age);

Asynchronous Operations

Just like waiting for your favorite show to buffer, Freezer permits asynchronous operations ensuring your app stays responsive:

userEntityManager.addAsync(users)
    .async(new SimpleCallback>() {
        @Override
        public void onSuccess(List data) {
            // Handle success
        }
    });

Updating and Ignoring Fields

You can also update or ignore certain fields in your models as needed:

user.setName("laurent");
userEntityManager.update(user);

Troubleshooting Common Issues

If you encounter issues while using Freezer, consider the following troubleshooting ideas:

  • Ensure that you have initialized Freezer correctly in your Application class.
  • Double-check your annotations on model classes; improper annotations can lead to persistence issues.
  • In case of query failures, verify the queries and the fields you are targeting.
  • If you face asynchronous errors, ensure that you’re managing threads properly especially when interacting with UI components.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox