Android Studio上的领域:如何检查交易成功或失败?

时间:2018-10-23 07:46:32

标签: database android-studio realm

我有以下代码,我想检查交易的成功/失败以生成特定的Toast消息。谢谢!

        buttonOK.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            realm.beginTransaction();

            transactions obj = realm.createObject(transactions.class);
            obj.setOperatrice(txtoperatrice.getText().toString());
            obj.setOperation(txtoperation.getText().toString());

            realm.commitTransaction();
        }
    });

1 个答案:

答案 0 :(得分:1)

您可以像这样使用回调-

realm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm bgRealm) {
            User user = bgRealm.createObject(User.class);
            user.setName("John");
            user.setEmail("john@corporation.com");
        }
    }, new Realm.Transaction.OnSuccess() {
        @Override
        public void onSuccess() {
            // Transaction was a success.
        }
    }, new Realm.Transaction.OnError() {
        @Override
        public void onError(Throwable error) {
            // Transaction failed and was automatically canceled.
        }
    });

希望它会对您有所帮助。