gpt4 book ai didi

android - Realm 异常 "nested transaction not allowed"即使没有嵌套事务

转载 作者:行者123 更新时间:2023-11-29 17:17:15 25 4
gpt4 key购买 nike

最近我开始了一个安卓应用项目。我需要尽可能结构化地管理数据。

所以我选择了 Realm 并对其进行了一些修改。

但是我遇到了一些错误。但是我不知道为什么会出现这个错误。

错误是

不允许嵌套事务。在每个 beginTransaction() 之后使用 commitTransaction()。

我的代码在下面。

public class CuratorApplication extends Application {

private Realm realm;

@Override
public void onCreate() {
super.onCreate();
Log.e("debug", "Application class create!!");
configureRealmDatabase(this);
}

private void configureRealmDatabase(Context context){
RealmConfiguration config = new RealmConfiguration.Builder(context)
.name("curator.realm")
.build();
Realm.setDefaultConfiguration(config);
}
}

我按照 here 中的描述在 Application 类中注册 Realm

我尝试在 Activity 中进行交易。但它显示错误。 :(

package com.nolgong.curator.screen;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.nolgong.curator.R;
import com.nolgong.curator.model.retrofit.Game;
import com.nolgong.curator.model.retrofit.GameInformation;
import com.nolgong.curator.model.retrofit.Team;
import com.nolgong.curator.network.NetworkClient;

import java.io.IOException;
import java.util.Properties;

import io.realm.Realm;
import io.realm.exceptions.RealmPrimaryKeyConstraintException;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class IntroActivity extends AppCompatActivity {

private Button confirmBtn;
private EditText confirmText;
private Realm realm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
setUp();
registerListener();
}

private String getProperty() throws IOException{
Properties properties = new Properties();
properties.load(getResources().openRawResource(R.raw.config));
String property = properties.getProperty("serverAddress");
Log.e("debug", "Property : " + property);
return property;
}

private void setNetworkClient(String serverAddress){
Log.e("debug", "Address : " + serverAddress);
NetworkClient.getInstance(serverAddress);
}

private void setUp(){
try {
setNetworkClient(getProperty());
} catch (IOException e){
Log.e("debug", "set network" + e);
}
confirmBtn = (Button)findViewById(R.id.intro_confirm);
confirmText = (EditText)findViewById(R.id.intro_input);
realm = Realm.getDefaultInstance();
Log.e("debug", "transaction state : " + realm.isInTransaction());
Log.e("debug", "CONFIGURATION : \n" + realm.getConfiguration());
}

private void registerListener(){
confirmBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String teamId = confirmText.getText().toString();
Integer digit = Integer.valueOf(teamId);
Log.e("debug", digit + "");
NetworkClient.getInstance().login(digit, new Callback<GameInformation>() {
@Override
public void onResponse(Call<GameInformation> call, Response<GameInformation> response) {
int responseCode = response.code();
switch (responseCode){
case 200:
GameInformation gameInformation = response.body();

Log.e("debug", "game information " + gameInformation.toString());

Game game = gameInformation.getGame();
Team team = gameInformation.getTeam();

updateGameToRealm(game);
updateTeamToRealm(team);
break;
default:
Log.e("debug", "Maybe something happened.");
break;
}
}

@Override
public void onFailure(Call<GameInformation> call, Throwable t) {
Log.e("debug", "Login fail :" + t.toString());
}
});
}
});
}

private void updateGameToRealm(Game game){

com.nolgong.curator.model.database.Game rGame = new com.nolgong.
curator.model.database.Game(game.getId(), game.getDate(),
game.getSession(), game.getRunningTime());
realm.beginTransaction();

try {
realm.copyToRealm(rGame);
} catch (RealmPrimaryKeyConstraintException e){
Log.e("debug", e.toString());
realm.cancelTransaction();
} finally {
realm.commitTransaction();
}
}

private void updateTeamToRealm(Team team){
com.nolgong.curator.model.database.Team rTeam = new com.nolgong.
curator.model.database.Team(team.getId(), team.getMembers(),
team.getGameId(), team.isClientDataSynced(),
team.getJob(), team.getDigit(),
team.getPoint());
realm.beginTransaction();
try {
realm.copyToRealm(rTeam);
} catch (RealmPrimaryKeyConstraintException e){
Log.e("debug", e.toString());
realm.cancelTransaction();
} finally {
realm.commitTransaction();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
}
}

为什么 realm 显示错误?我使用正确吗?或者这只是一个错误?请帮我 ioi..

最佳答案

如错误所述,

Nested transactions are not allowed. Use commitTransaction() after each beginTransaction().

这意味着你不能做这样的事情:

realm.beginTransaction();
...
realm.beginTransaction();
realm.commitTransaction();

beginTransaction() 调用后必须跟有 commitTransaction()cancelTransaction() 调用。

还强烈建议使用 executeTransaction() 而不是 begin/cancel/commit,因为它更易于使用,并且会自动为您处理取消异常。

编辑:您不应该在使用 cancelTransaction() 回滚事务后提交事务。

请尝试用 executeTransaction() 替换 begin/cancel/commit 看看会发生什么。

此外,您可以尝试将 copyToRealm() 替换为 copyToRealmOrUpdate()

我认为您可能遇到了 this issue因为 UI 线程上的多个事务你遇到了失败,但实际上我不确定。

编辑 2:

private void updateGameToRealm(Game game){
Realm realm = null;
try {
realm = Realm.getDefaultInstance();
final com.nolgong.curator.model.database.Game rGame = new com.nolgong.
curator.model.database.Game(game.getId(), game.getDate(),
game.getSession(), game.getRunningTime());
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.copyToRealmOrUpdate(rGame);
}
});
} finally {
if(realm != null) {
realm.close();
}
}
}

private void updateTeamToRealm(Team team){
Realm realm = null;
try {
realm = Realm.getDefaultInstance();
final com.nolgong.curator.model.database.Team rTeam = new com.nolgong.
curator.model.database.Team(team.getId(), team.getMembers(),
team.getGameId(), team.isClientDataSynced(),
team.getJob(), team.getDigit(),
team.getPoint());
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.copyToRealmOrUpdate(rTeam);
}
});
} finally {
if(realm != null) {
realm.close();
}
}
}

关于android - Realm 异常 "nested transaction not allowed"即使没有嵌套事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38306282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com