Best Practices for CozyData

A comprehensive guide to best practices for using CozyData in your Flutter applications.

Initialize Early

To ensure optimal performance and data readiness, it is essential to call CozyData.initialize() as early as possible in your app's lifecycle. This initialization step sets up the data management system, making it ready for use when your app starts.

void main() async {
  // Ensure Flutter's binding is initialized before any async operations
  WidgetsFlutterBinding.ensureInitialized();

  // Initialize CozyData with precise, intentional configuration
  await CozyData.initialize(
    // Select an appropriate storage engine based on your performance and complexity requirements
    engine: CozyEngine.sqlite3,
    
    // Register necessary data mappers to enable seamless object-to-database transformations
    mappers: [RecipeMapper.ensureInitialized()]
  );

  // Launch the application with a fully prepared data management infrastructure
  runApp(MyApp());
}

Dispose Queries

Memory management is crucial for maintaining the performance and stability of your application. Always dispose of queries when they are no longer needed to prevent memory leaks. This practice ensures that your application remains efficient and responsive.

final CozyQueryListener<Recipe> _recipeQuery = CozyData.queryListener<Recipe>();


void dispose() {
 // This prevents memory leaks and frees up system resources   
 _recipeQuery.dispose();
  super.dispose();
}

Annotations

Primary Key in Models

Ensure that your models always include an persistentModelID field, which serves as the primary key for data persistence. This field is essential for uniquely identifying records in your database.

import 'package:dart_mappable/dart_mappable.dart';
part 'recipe.mapper.dart';

()
class Recipe with RecipeMappable {
  final String persistentModelID; // This field is required for the cozy_data package to work
  String name;
  Recipe({required this.id, required this.name});
}

Custom Primary Key

If you need a custom primary key, follow these steps:

1

Step 1: Define your model

Create your model with the custom primary key.

import 'package:dart_mappable/dart_mappable.dart';
part 'recipe.mapper.dart';

()
class Recipe with RecipeMappable {
final int customPersistentModelID;// This will be the new primary key
String name;
Recipe({required this.customPersistentModelID, required this.name});
}
2

Step 2: Set CozyData Persistent Model ID

Initialize CozyData with your custom primary key as the persistentModelID.

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await CozyData.initialize(
  engine: CozyEngine.sqlite3,
  persistentModelID: "customPersistentModelID",
  mappers: [RecipeMapper.ensureInitialized()]);
runApp(const MyApp());
}

‼️Important‼️:

Choose the right primary key carefully, as CozyData will create tables with your custom key as the primary key for all persistence data models. Currently, it is advisable to avoid custom primary keys if possible. The package will support custom IDs for each data model in the future. Follow the GitHub repository for updates on this feature.