您好我正在尝试根据executeTransactionAsync中的switch case将数据写入两个表。我收到错误
05-31 14:37:29.811 22582-22582/com.app.attendancesystem W/System.err: java.lang.ClassCastException: io.realm.EmployeeRealmProxy cannot be cast to model.EmpTimeLog
05-31 14:37:29.811 22582-22582/com.app.attendancesystem W/System.err: at util.DatabaseOperation$9.execute(DatabaseOperation.java:269)
05-31 14:37:29.811 22582-22582/com.app.attendancesystem W/System.err: at io.realm.Realm$2.run(Realm.java:1295)
05-31 14:37:29.811 22582-22582/com.app.attendancesystem W/System.err: at io.realm.internal.async.BgPriorityRunnable.run(BgPriorityRunnable.java:34)
05-31 14:37:29.811 22582-22582/com.app.attendancesystem W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
05-31 14:37:29.811 22582-22582/com.app.attendancesystem W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-31 14:37:29.811 22582-22582/com.app.attendancesystem W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-31 14:37:29.811 22582-22582/com.app.attendancesystem W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-31 14:37:29.811 22582-22582/com.app.attendancesystem W/System.err: at java.lang.Thread.run(Thread.java:841)
这是我的executeTransactionAsync代码
Realm realm = Realm.getDefaultInstance();
try {
cancelAsync();
asyncTransaction = realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm bgRealm) {
switch (tag) {
case Config.TAG_EMP: {
ArrayList<Employee> mArrEmp = (ArrayList<Employee>) mArrObj;
Config.debug("Arr size inside " + mArrEmp.size());
for (int i = 0; i < mArrEmp.size(); i++) {
int id;
if (bgRealm.where(Employee.class).max("id") == null) {
id = 1;
} else {
id = bgRealm.where(Employee.class).max("id").intValue() + 1;
}
Config.error("Id val " + id);
mArrEmp.get(i).setId(id);
bgRealm.copyToRealmOrUpdate(mArrEmp.get(i));
}
break;
}
case Config.TAG_LOG:{
ArrayList<EmpTimeLog> mArrEmpTimeLog = (ArrayList<EmpTimeLog>) mArrObj;
Config.debug("Arr size inside " + mArrEmpTimeLog.size());
for (int i = 0; i < mArrEmpTimeLog.size(); i++) {
int id;
if (bgRealm.where(EmpTimeLog.class).max("logId") == null) {
id = 1;
} else {
id = bgRealm.where(EmpTimeLog.class).max("logId").intValue() + 1;
}
Config.error("Id val " + id);
mArrEmpTimeLog.get(i).setLogId(id);
bgRealm.copyToRealmOrUpdate(mArrEmpTimeLog.get(i));
}
break;
}
default:
break;
}
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
mReceiver.onCompleteInsertion(tag, msg);
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
error.printStackTrace();
mReceiver.onDatabaseError(Config.DATABASE_INSERT_ERROR, "Error while inserting " + error.getMessage());
}
});
} catch (Exception ex) {
ex.printStackTrace();
mErrorOccured = true;
Config.error("Error occurred " + ex.getMessage());
}
模型文件:
public class Employee extends RealmObject {
@PrimaryKey
private int id=1;
private String empName="";
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
public class EmpTimeLog extends RealmObject{
@PrimaryKey
private int logId=1;
private Date signInTime;
private Date signOutTime;
private int empId;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public int getLogId() {
return logId;
}
public void setLogId(int logId) {
this.logId = logId;
}
public Date getSignInTime() {
return signInTime;
}
public void setSignInTime(Date signInTime) {
this.signInTime = signInTime;
}
public Date getSignOutTime() {
return signOutTime;
}
public void setSignOutTime(Date signOutTime) {
this.signOutTime = signOutTime;
}
}
我的疑问是:我是否需要为每次写入编写单独的异步事务?