简体   繁体   English

如何在Realm Table Android中更新特定行

[英]How to update a particular row in realm table android

I have the following table with a PrimaryKey in it. 我有一个带有PrimaryKey的下表。 I have inserted some values in the table. 我在表中插入了一些值。 Now I need to update a particular value in a particular row. 现在,我需要在特定行中更新特定值。 I have a row with gameType as Puzzle and I need to update the currentLevel in the row. 我有一排gameTypePuzzle ,我需要在该行中更新currentLevel But I am not able to achieve that. 但我无法实现这一目标。

GamesDetails table: GamesDetails表:

public class GamesDetail extends RealmObject {
    @PrimaryKey
    private String gameType;
    private int currentLevel;
    private int totalLevel;
    private int totalCoins;
    private int currentBadge;

    public String getGameType() {
        return gameType;
    }

    public void setGameType(String gameType) {
        this.gameType = gameType;
    }

    public int getCurrentLevel() {
        return currentLevel;
    }

    public void setCurrentLevel(int currentLevel) {
        this.currentLevel = currentLevel;
    }

    public int getTotalLevel() {
        return totalLevel;
    }

    public void setTotalLevel(int totalLevel) {
        this.totalLevel = totalLevel;
    }

    public int getTotalCoins() {
        return totalCoins;
    }

    public void setTotalCoins(int totalCoins) {
        this.totalCoins = totalCoins;
    }

    public int getCurrentBadge() {
        return currentBadge;
    }

    public void setCurrentBadge(int currentBadge) {
        this.currentBadge = currentBadge;
    }
}

Here is what I have tried to update a particular row in the table: 这是我尝试更新表中特定行的方法:

final GamesDetail puzzleGameDetail = realm.where(GamesDetail.class).equalTo("gameType","Puzzle").findFirst();
                    final int[] nextLevel = {puzzleGameDetail.getCurrentLevel()};

                    realm.executeTransactionAsync(new Realm.Transaction() {
                        @Override
                        public void execute(Realm realm) {
                            puzzleGameDetail.setCurrentLevel(++nextLevel[0]);
                            realm.copyToRealmOrUpdate(puzzleGameDetail);
                        }
                    }, new Realm.Transaction.OnSuccess() {
                        @Override
                        public void onSuccess() {
                            Log.e(TAG, "Done");
                        }
                    }, new Realm.Transaction.OnError() {
                        @Override
                        public void onError(Throwable error) {
                            Log.e(TAG,error.getMessage());
                        }
                    });

But the value is not getting updated and I am getting this following error: 但是该值未更新,并且出现以下错误:

Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.

How can I update a particular value in a particular row in the table ? 如何更新表中特定行中的特定值?

When calling executeTransactionAsync , the execute block will run in a background thread, any Realm objects access from that thread need to be created/queried on that thread from the Realm instance which is the param of execute . 当调用executeTransactionAsyncexecute块将在后台线程中运行,需要从Realm实例(这是execute的参数)在该线程上创建/查询对该线程的任何Realm对象访问。

Move your finding GamesDetail query inside execute block and rest will work fine. 将您发现的GamesDetail查询移至execute块内,其余代码将正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM