gpt4 book ai didi

java - 互联网关闭时正在删除数据

转载 作者:行者123 更新时间:2023-11-29 02:32:00 24 4
gpt4 key购买 nike

我正在使用 realm compile 'io.realm:realm-android:0.82.1' 如果互联网正常,数据将被成功保存和检索。但如果我关闭设备的互联网,它不会返回数据。有人可以提出建议吗?

我已点击此链接了解 Realm 。 https://www.androidhive.info/2016/05/android-working-with-realm-database-replacing-sqlite-core-data/

这是我的应用程序类代码

@Override
public void onCreate() {
super.onCreate();

RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
.name(Realm.DEFAULT_REALM_NAME)
.schemaVersion(0)
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(realmConfiguration);

下面是 realmcontroller,它配备了一些我用来插入或检索数据的代码

public class RealmController {

private static RealmController instance;
private final Realm realm;

public RealmController(Application application) {
realm = Realm.getDefaultInstance();
}

public static RealmController with(Fragment fragment) {

if (instance == null) {
instance = new RealmController(fragment.getActivity().getApplication());
}
return instance;
}

public static RealmController with(Activity activity) {

if (instance == null) {
instance = new RealmController(activity.getApplication());
}
return instance;
}

public static RealmController with(Application application) {

if (instance == null) {
instance = new RealmController(application);
}
return instance;
}

public static RealmController getInstance() {

return instance;
}

public Realm getRealm() {

return realm;
}

//Refresh the realm istance
public void refresh() {

realm.refresh();
}

//clear all objects from Book.class
public void clearAll() {

realm.beginTransaction();
realm.clear(ServiceModel.class);
realm.commitTransaction();
}

//find all objects in the Book.class
public RealmResults<ServiceModel> getAllServices() {
return realm.where(ServiceModel.class).findAll();
}

//query a list of services by category_id
public RealmResults<ServiceModel> getServicesByCategory(String id) {
return realm.where(ServiceModel.class).equalTo("category_id", id).findAll();
}

//query a list of services by category_id
public ServiceDetailsModel getServiceDetails(String service_id) {
return realm.where(ServiceDetailsModel.class).equalTo("service_id", service_id).findFirst();
}

//query a list of services by category_id
public void clearServicesByCategory(final String id) {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmResults<ServiceModel> rows = realm.where(ServiceModel.class).equalTo("category_id", id).findAll();
rows.clear();
}
});
}

// save a model in realm
public <M extends RealmObject> void saveModel(M object) {
realm.beginTransaction();
realm.copyToRealm(object);
realm.commitTransaction();
}

//check if Book.class is empty
public boolean hasBooks() {
return !realm.allObjects(ServiceModel.class).isEmpty();
}

//query example
public RealmResults<ServiceModel> queryedBooks() {

return realm.where(ServiceModel.class)
.contains("author", "Author 0")
.or()
.contains("title", "Realm")
.findAll();

}
}

最佳答案

不确定这是否会解决您的问题,但请尝试在设置配置之前添加 Realm.init(this);

关于java - 互联网关闭时正在删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49152281/

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