jonataslaw / get_storage

A fast, extra light and synchronous key-value storage to Get framework
MIT License
342 stars 81 forks source link

GetStorage containers must be initialised individually for data to sync to disk #103

Open lhengl opened 2 years ago

lhengl commented 2 years ago

This took me a few hours to debug, however, it appears that unlike Hive, the container of each GetStorage must be initialised before it can be used. Is this intended?

Based on the documentation, I assumed that GetStorage.init() was to be run once in main(). This seemed to work with one container. But when I started working with multiple containers, the second container can read/write to memory, but after the app is restarted, container.read() does not find any value previously written to memory. Which indicates that it hasn't stored the data on disk.

Then as I went through the underlying code, I found that GetStorage.init() has a container parameter. Sure enough as soon as I initialised the containers by calling GetStorage('container1'); and GetStorage('container2'); then those containers actually work across restarts.

Then it came to a problem of naming those containers by network data. For example, I want to be able to store information by users in different containers. This information is not available in main(). So I had to run GetStorage.init('container') in all my controller's init. I know it's only two lines, but it's verbose. Imo, calling GetStorage('container') should initialise the container if it hasn't yet initialised.

Currently this works for me:

class Controller extends GetxController {
    @override
    void onInit() async {
          var storageContainer = '${UserController.to.currentUser.id}';
          await GetStorage.init(storageContainer);
          storage = GetStorage(storageContainer);
    }
}

Removing the init line will not work across restarts, even if I have GetStorage.init() in main(). Which init only the default 'GetStorage' container it seems. Funny enough, the first container regardless of name works! So if I use GetStorage('container1') with GetStorage.init() in main(), it works across restarts too... To be sure of this, I kept renaming the first container to different names, and it works even if they aren't using the default 'GetStorage' name.

Is this intended? The document doesn't go into too much details regarding containers.

d-apps commented 2 years ago

If you need more than one container you need to initialize them inside main.

I always try to have one container and organize data inside this single container like having different keys for different data I need to persist.

You could a different approach, for example, I understood that you would have a container for each user but you could have one container for all users and organize their data using keys, actually it could be the default container, you didn't even need to to name the default container.

Keys could be their ids.

void main() async {

await GetStorage.init(); // it will initialize the default container, which is called "GetStorage".

// Somewhere in the app final getStorage = GetStorage();

getStorage.write("userId", userData);

final data = getStorage.read("userId");

}

Unfortunately I don't think you can initialize containers in runtime, dynamically.

lahirunc commented 2 years ago

Same issue... data cannot be read after app restarts

DemoJameson commented 2 years ago

I think the documentation should be supplemented with the use of multiple containers, I also wasted several hours on this.

Funny enough, the first container regardless of name works! So if I use GetStorage('container1') with GetStorage.init() in main(), it works across restarts too

This should be treated as a bug.

fengerwoo commented 1 year ago

I also think it is necessary to write this part in a conspicuous place in the document. It takes too much time to debug

electraport commented 1 year ago

Interesting that this is still open and not closed. It is certainly still an issue, as it caught me. I vote for being able to initialize within a GetXService, which was my intent, which service is responsible for all interactions with the filesystem/persistent storage. So in my use case main initializes service A which then initializes services B, C, D, E, etc. among which are the StorageService, where GetStorage.init() goes. Used to go.

prooshani commented 5 months ago

2 years passed and it is still a bug and lack enough information about using containers inside a box of GetStorage!

I know this package not supporting indexing but, I need to store several key-value information for an object and need to contain them inside a container. Regarding the nature of my request which is a dynamic key-value storage I need to know how I have to manage them using Box/Container which should not need to initiate every single time the app restarts.