【发布时间】:2017-09-19 09:58:00
【问题描述】:
我有下表,其中包含PrimaryKey。我在表中插入了一些值。现在我需要更新特定行中的特定值。我有一排gameType 为Puzzle,我需要更新该行中的currentLevel。但我无法做到这一点。
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;
}
}
这是我尝试更新表中特定行的内容:
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());
}
});
但是值没有得到更新,我收到以下错误:
Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
如何更新表中特定行中的特定值?
【问题讨论】: