}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
UIUtils.showToast(“删除失败”);
}
});
- 异步删除(推荐使用):delete(xxx.class)
//删除指定的数据库
realmAsyncTask = mRealm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.delete(Student.class);
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
UIUtils.showToast(“删除成功”);
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
UIUtils.showToast(“删除失败”);
}
});
- 推荐使用:异步查询,同步删除
students8 = mRealm.where(Student.class).findAllAsync();
students8.addChangeListener(callback);
/**
- 异步操作的监听
*/
private OrderedRealmCollectionChangeListener<RealmResults> callback = new OrderedRealmCollectionChangeListener<RealmResults>() {
@Override
public void onChange(RealmResults collection, OrderedCollectionChangeSet changeSet) {
if (changeSet == null) {
//第一次异步返回一个空的变更集。
mRealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.delete(Student.class);
}
});
} else {
//每次RealmResults被更新时都会被调用
}
}
};
if (students8 != null) {
students8.removeChangeListener(callback);
students8.removeAllChangeListeners();
}
- 推荐使用:异步查询,异步删除
students9 = mRealm.where(Student.class).findAllAsync();
students9.addChangeListener(callback1);
/**
* 异步操作的监听
*/
private OrderedRealmCollectionChangeListener
if (students9 != null) {
students9.removeChangeListener(callback);
students9.removeAllChangeListeners();
}
[Realm数据库使用教程(六):数据迁移](()
[Demo地址:https://gitee.com/huangxiaoguo/Realm](()
avticity全部代码如下:
package tsou.com.simple.realmtest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import io.realm.Order