Szczypiorek
Szczypiorek

Reputation: 188

DBFlow Transactions Overhaul - need to rewrite my code

As with the new version of Raizlabs DBFlow the way of managing databases has been changed.

Right now, after hours of reading/learing I'm pressed to the wall. I need to rewrite two methods from old version of DBFlow (3.0.0-beta5) to new (4.0.0-beta7):

public static void saveAll(Collection<? extends Model> models) {

    TransactionManager.getInstance().addTransaction(new SaveModelTransaction<>(ProcessModelInfo.withModels(models)));

}


public static void deleteAll(Collection<? extends Model> models) {

    TransactionManager.getInstance().addTransaction(new DeleteModelListTransaction<>(ProcessModelInfo.withModels(models)));

}

I have seen this article: https://github.com/Raizlabs/DBFlow/blob/master/usage2/Migration3Guide.md#transactions-overhaul but still I can't do this. This is my first contact with ORM and I just don't understand it very well

Upvotes: 0

Views: 406

Answers (1)

Szczypiorek
Szczypiorek

Reputation: 188

The correct answer is:

    public static void saveAll(final Collection<? extends Model> models) {
    FlowManager.getDatabase(NAME)
            .beginTransactionAsync(new ProcessModelTransaction.Builder<>(
                    new ProcessModelTransaction.ProcessModel<Model>() {
                        @Override
                        public void processModel(Model model, DatabaseWrapper wrapper) {
                            model.save();
                        }
                    }).addAll(models).build())
            .error(new Transaction.Error() {
                @Override
                public void onError(Transaction transaction, Throwable error) {

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

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

and

    public static void deleteAll(final Collection<? extends Model> models) {

    FlowManager.getDatabase(NAME)
            .beginTransactionAsync(new ProcessModelTransaction.Builder<>(
                    new ProcessModelTransaction.ProcessModel<Model>() {
                        @Override
                        public void processModel(Model model, DatabaseWrapper wrapper) {
                            model.delete();
                        }
                    }).addAll(models).build())
            .error(new Transaction.Error() {
                @Override
                public void onError(Transaction transaction, Throwable error) {

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

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

}

Upvotes: 2

Related Questions