尝试执行executeTransactionAsync时,它在Realm中返回错误 [英] When trying to executeTransactionAsync, it returns error in Realm
问题描述
当尝试执行TransactionAsync时,它返回错误,例如'MODEL =方法抛出'java.lang.IllegalStateException'异常.无法评估io.realm.ModelRealmProxy.toString()'.另外,当我获取并调试它时,它会在图像下方显示类似
When trying to executeTransactionAsync, it returns error like 'MODEL = Method threw 'java.lang.IllegalStateException' exception. Cannot evaluate io.realm.ModelRealmProxy.toString()'. Also when I am fetching and debug it, it displays data something like below image
public class AsyncTaskExample extends AppCompatActivity {
List<Student> mStudentCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_async_task_example);
mStudentCount = new ArrayList<>();
((Button) findViewById(R.id.insert_10)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
final Realm mRealm = Realm.getDefaultInstance();
try{
Log.i(getClass().getSimpleName(), "----------------------------before start :: " +
mRealm.where(Student.class).findAll().size());
mRealm.executeTransaction(
new Realm.Transaction() {
@Override
public void execute(Realm realm) {
for(int i=0;i<10;i++){
Log.i(getClass().getSimpleName(), "----------------------------inserting data");
String s = Utils.randomAlphaNumeric(8);
Log.i(getClass().getSimpleName(), "------------------------name : " + s);
Student student = realm.createObject(Student.class);
student.setRollNo(String.valueOf(AppClass.ROLL_NO++));
student.setName(s);
mStudentCount.add(student);
}
realm.copyToRealmOrUpdate(mStudentCount);
}
}
);
}
catch (Exception e){
System.out.println(e.toString());;
}
finally {
mRealm.close();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Log.i(getClass().getSimpleName(), "----------------------------after success :: " +
RealmHClass.getRealm().where(Student.class).findAll().size());
}
}.execute();
}
});
((Button) findViewById(R.id.read_in_async)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AsyncTask<Void, Integer, List<Student>>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected List<Student> doInBackground(Void... params) {
Realm mRealm = Realm.getDefaultInstance();
Integer i = Integer.valueOf(0);
try{
RealmList<Student> s = new RealmList<Student>();
mStudentCount.addAll(mRealm.where(Student.class).findAll());
}
catch (Exception e){
}
finally {
mRealm.close();
}
return mStudentCount;
}
@Override
protected void onPostExecute(List<Student> integer) {
super.onPostExecute(integer);
Toast.makeText(AsyncTaskExample.this, "Total data : " + integer.size(), Toast.LENGTH_LONG).show();
}
}.execute();
}
});
}
}
public class Student extends RealmObject {
@PrimaryKey
String rollNo;
String name;
public Student() {
}
public String getRollNo() {
return rollNo;
}
public void setRollNo(String rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"rollNo='" + rollNo + '\'' +
", name='" + name + '\'' +
'}';
}
}
public class AppClass extends Application {
public static RealmConfiguration realmConfig;
public static AppClass instance;
public static int ROLL_NO;
@Override
public void onCreate() {
super.onCreate();
instance = this;
ROLL_NO =0;
realmConfig = new RealmConfiguration.Builder(this)
.build();
Realm.setDefaultConfiguration(realmConfig);
}
public static void deleteRealmDatabase(){
Log.i("AppClass","delete realm file : " + Realm.deleteRealm(realmConfig));
}
public static AppClass getInstance() {
return instance;
}
}
public class Utils {
private static final String ALPHA_NUMERIC_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
public static String randomAlphaNumeric(int count) {
StringBuilder builder = new StringBuilder();
while (count-- != 0) {
int character = (int) (Math.random() * ALPHA_NUMERIC_STRING.length());
builder.append(ALPHA_NUMERIC_STRING.charAt(character));
}
return builder.toString();
}
}
推荐答案
我遇到了同样的错误.从您的代码中可以看到,我的RealmObject中也有toString().因此,我去掉了它,并解决了问题.那是一个快速的解决方案,因为这种方法会引起问题.
I was getting the same error. And from what I see in your code, I also had the toString() in my RealmObject. So I went and removed it, and that fixed the problem. That was a quick solution, as that method is causing the problem.
这篇关于尝试执行executeTransactionAsync时,它在Realm中返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!