【问题标题】:Realm on Android Studio : How can I check transaction success or failure?Android Studio 上的领域:如何检查交易成功或失败?
【发布时间】:2023-03-29 14:00:01
【问题描述】:

我有以下代码,我想检查我的事务是否成功/失败以生成特定的 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();
        }
    });

【问题讨论】:

    标签: database android-studio realm


    【解决方案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.
            }
        });
    

    希望对您有所帮助。

    【讨论】:

    • 欢迎您。你能接受我的回答吗? @MohammedZegui
    • 当然,完成! :) :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-16
    • 1970-01-01
    • 2018-02-16
    • 2023-02-07
    • 2019-06-19
    • 2012-09-22
    • 1970-01-01
    相关资源
    最近更新 更多