agrosner / DBFlow

A blazing fast, powerful, and very simple ORM android database library that writes database code for you.
MIT License
4.87k stars 598 forks source link

Save list #996

Closed Reginer closed 8 years ago

Reginer commented 8 years ago

DBFlow Version: 3.1.1 Question

Description:

Hey guys。

I want to save a list like 3.0.0beta5

new SaveModelTransaction<>(ProcessModelInfo.withModels(users)).onExecute();

how I should do in 3.1.1 ?

Have a simple code not like

FlowManager.getDatabase(MyDatabase.class) .beginTransactionAsync(new ProcessModelTransaction.Builder<>( new ProcessModelTransaction.ProcessModel() { @Override public void processModel(FindContent.FindContentBean content) { // do work here -- i.e. user.delete() or user.update() content.save(); } }).addAll(event.getData()).build()) // add elements (can also handle multiple) .error(new Transaction.Error() { @Override public void onError(Transaction transaction, Throwable error) {

                    }
                })
                .success(new Transaction.Success() {
                    @Override
                    public void onSuccess(Transaction transaction) {

                    }
                }).build().execute();

Too cumbersome

trevjonez commented 8 years ago

You can do it synchronously.

FlowManager.getDatabase(MyDatabase.class)
           .executeTransaction(databaseWrapper -> {
               for (MyModel model : models) {
                   model.save();
               }
           });

or consider using the save queue thread if it isn't critical that they are saved at exactly that moment.

FlowManager.getDatabase(MyDatabase.class)
           .getTransactionManager()
           .getSaveQueue()
           .addAll(models);

Keep in mind that you can customize the save queue or explicitly flush it after calling addAll

Reginer commented 8 years ago

In

FlowManager.getDatabase(MyDatabase.class) .getTransactionManager() .getSaveQueue() .addAll(models); Error is

addAll (java.util.Collection) in DBBatchSaveQueue cannot be applied to (java.util.List)

The method addAll can not be ArrayList?

 

trevjonez commented 8 years ago

You are correct the method in question has an issue with type signature. It should be: addAll(final Collection<? extends Model> list) but it is: addAll(final Collection<Model> list)

trevjonez commented 8 years ago

As a workaround for now you can wrap the array list. so the current .addAll(models) would become .addAll(new ArrayList<>(models)) One of us should put in a PR that fixes it, although I believe this would be a binary compatibility breaking change which depending on how @agrosner feels may not be released too soon.

You could also for the repo, make the change and request the dependencies though jitpack using your own username and commit hash as the version.

Reginer commented 8 years ago

I see,thanks.

agrosner commented 8 years ago

Itll be fixed for when 4.0.0-beta1 is released. in develop now.