0

与新版本的 Raizlabs DBFlow 一样,管理数据库的方式也发生了变化。

现在,经过几个小时的阅读/学习,我被逼到了墙角。我需要将两种方法从旧版本的 DBFlow(3.0.0-beta5)重写为新版本(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)));

}

我看过这篇文章:https ://github.com/Raizlabs/DBFlow/blob/master/usage2/Migration3Guide.md#transactions-overhaul但我仍然不能这样做。这是我第一次接触ORM,只是不太了解

4

1 回答 1

2

正确答案是:

    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();
}

    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();

}
于 2017-04-19T10:37:32.613 回答