ModelCache<T extends DBModelI> constructor

ModelCache<T extends DBModelI>()

Create a memory cache for Models. For this module to work correctly Firebase cache has to be turned on. (It is automatically on unless you change Firebase configuration to turn it off)

This will store the fetches done through here in memory.

If it is not in memory, this loads data from firebase cache. And will store the data in memory for later fetches.

If it is not available in firebase cache also it will get the data from online Firestore and save in memory for later user.

In any of the above cases, if the data fetched is older than _timeout minutes, it will send a request to get the data from online Firestore from the background and store it in the memory. So the data will be available in the next time you fetch.

Before using ModelCache with a specific DBModelI implementation, configurations for that specific DBModelI has to be set through ModelCacheSettings. Otherwise this will throw an Exception.

See Also:

ModelCacheSettings - Store the setting information for each DBModelI RepositoryAddon - Used to fetch data from DocumentReference FirebaseRepository - Used to fetch data from Firestore and wrap them in models.

Implementation

factory ModelCache() {
  final name = T.toString();
  if (_instances[name] == null) {
    final timeout = _timeouts[name];
    final addon = _addons[name];
    if (timeout == null || addon == null) {
      throw Exception("Cannot find settings for the DBModelI type $T. "
          "You can set settings for this type using ModelCacheSettings. "
          "Make sure to use the correct type when calling the setSettings "
          "function of that class.\n"
          "Eg: ModelCacheSettings.setSettings(...) is not the correct way "
          "of adding settings.\n"
          "To add settings to type User "
          "(which extends DBModelI) you can use\n"
          "Eg: ModelCacheSettings.setSettings<User>(...) \n\n"
          "If the issue still persists, use our github to create a issue.\n"
          "https://github.com/fcodelabs/fcode_bloc/issues");
    }
    _instances[name] = ModelCache<T>._(timeout, addon as RepositoryAddon<T>);
  }
  return _instances[name] as ModelCache<T>;
}