use of com.amplifyframework.core.model.ModelProvider in project amplify-android by aws-amplify.
the class SQLiteStorageAdapter method initialize.
/**
* {@inheritDoc}
*/
@Override
public synchronized void initialize(@NonNull Context context, @NonNull Consumer<List<ModelSchema>> onSuccess, @NonNull Consumer<DataStoreException> onError, @NonNull DataStoreConfiguration dataStoreConfiguration) {
Objects.requireNonNull(context);
Objects.requireNonNull(onSuccess);
Objects.requireNonNull(onError);
// Create a thread pool large enough to take advantage of parallelization, but small enough to avoid
// OutOfMemoryError and CursorWindowAllocationException issues.
this.threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * THREAD_POOL_SIZE_MULTIPLIER);
this.context = context;
this.dataStoreConfiguration = dataStoreConfiguration;
threadPool.submit(() -> {
try {
/*
* Start with a fresh registry.
*/
schemaRegistry.clear();
/*
* Create {@link ModelSchema} objects for the corresponding {@link Model}.
* Any exception raised during this when inspecting the Model classes
* through reflection will be notified via the `onError` callback.
*/
schemaRegistry.register(modelsProvider.modelSchemas(), modelsProvider.customTypeSchemas());
/*
* Create the CREATE TABLE and CREATE INDEX commands for each of the
* Models. Instantiate {@link SQLiteStorageHelper} to execute those
* create commands.
*/
this.sqlCommandFactory = new SQLiteCommandFactory(schemaRegistry, gson);
CreateSqlCommands createSqlCommands = getCreateCommands(modelsProvider.modelNames());
sqliteStorageHelper = SQLiteStorageHelper.getInstance(context, databaseName, DATABASE_VERSION, createSqlCommands);
/*
* Create and/or open a database. This also invokes
* {@link SQLiteStorageHelper#onCreate(SQLiteDatabase)} which executes the tasks
* to create tables and indexes. When the function returns without any exception
* being thrown, invoke the `onError` callback.
*
* Errors are thrown when there is no write permission to the database, no space
* left in the database for any write operation and other errors thrown while
* creating and opening a database. All errors are passed through the
* `onError` callback.
*
* databaseConnectionHandle represents a connection handle to the database.
* All database operations will happen through this handle.
*/
databaseConnectionHandle = sqliteStorageHelper.getWritableDatabase();
/*
* Create helper instance that can traverse through model relations.
*/
this.sqliteModelTree = new SQLiteModelTree(schemaRegistry, databaseConnectionHandle);
/*
* Create a command processor which runs the actual SQL transactions.
*/
this.sqlCommandProcessor = new SQLCommandProcessor(databaseConnectionHandle);
sqlQueryProcessor = new SqlQueryProcessor(sqlCommandProcessor, sqlCommandFactory, schemaRegistry);
syncStatus = new SyncStatus(sqlQueryProcessor, dataStoreConfiguration);
/*
* Detect if the version of the models stored in SQLite is different
* from the version passed in through {@link ModelProvider#version()}.
* Delete the database if there is a version change.
*/
toBeDisposed.add(updateModels().subscribe(() -> onSuccess.accept(Immutable.of(new ArrayList<>(schemaRegistry.getModelSchemaMap().values()))), throwable -> onError.accept(new DataStoreException("Error in initializing the SQLiteStorageAdapter", throwable, AmplifyException.TODO_RECOVERY_SUGGESTION))));
} catch (Exception exception) {
onError.accept(new DataStoreException("Error in initializing the SQLiteStorageAdapter", exception, "See attached exception"));
}
});
}
Aggregations