gpt4 book ai didi

Android Realm占用大量数据空间

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

我目前正在开发一个使用 Realm 保存记录的 CRM Android 应用程序。我正在从网络服务器同步数据。有 10 个表。这个表有大约 450 页,这样每页包含 100 条记录(100 行的数据)获取 10 个表的记录后,我的应用程序的数据大小已增长到 118MB

Is this much space taking normal for Realm? ---> 118MB.

还是我在 Realm 上做错了什么?我已经检查了我的记录是否重复。没有重复,每一行只保存一次。

我就是这样配置 Realm 的应用类

realmConfig = new RealmConfiguration.Builder(this).build();
Realm.setDefaultConfiguration(realmConfig);

后来,我在任何需要的地方都使用了 Default 实例。并在 Application 类的 onTerminate 上关闭 Realm

更多信息:安卓工作室版本=2.1.3

Realm 版本 = 1.2.0

设备 = Nexus 5 Marshmallow

用于在数据库中保存记录的 My Intent 服务。每次页面响应后都会调用它。

public class SaveTableRecord extends Base_IntentService {

private static final String ACTION_SAVE_RECORDS = "saveRecords";
private static final String EXTRA_CLASS = "classVar";
private static final String EXTRA_TABLE_JSON = "tabJson";
private static final String LOG_TAG = "saveTableRecord";

private String json, className;
Realm realm;
private Gson gson;

public SaveTableRecord() {
super("SaveTableRecord");
}

public static void startSavingRecord(Context context, String cls, String tableJson) {
Log.i("Service", "SAVETABLERECORD.CLASS started");
Intent intent = new Intent(context, SaveTableRecord.class);
intent.setAction(ACTION_SAVE_RECORDS);
intent.putExtra(EXTRA_TABLE_JSON, tableJson);
intent.putExtra(EXTRA_CLASS, cls);
context.startService(intent);
}

@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
className = intent.getStringExtra(EXTRA_CLASS);
json = intent.getStringExtra(EXTRA_TABLE_JSON);
initGsonDataTypes();
initRecordSaving();
}
}


private void initGsonDataTypes() {
GsonBuilder builder = new GsonBuilder();
// builder.setLongSerializationPolicy(LongSerializationPolicy.STRING);
builder.registerTypeAdapter(long.class, new LongTypeAdapter());
builder.registerTypeAdapter(double.class, new DoubleTypeAdapter());
builder.registerTypeAdapter(int.class, new IntTypeAdapter());
builder.registerTypeAdapter(short.class, new ShortTypeAdapter());
builder.registerTypeAdapter(boolean.class, new BooleanTypeAdapter());
builder.registerTypeAdapter(Date.class, new GsonUTCDateAdapter());
builder.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaringClass().equals(RealmObject.class);
}

@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
});
builder.setPrettyPrinting();
gson = builder.create();
}

private void initRecordSaving() {
try {
realm = Realm.getDefaultInstance();
showLog("-------------------InitRecordSaving()--------------------");
final JSONObject main = new JSONObject(json);
final JSONArray jsonArray = main.getJSONArray("records");
// String json = main.getString("records");
// saveATableRecord(json);

realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {

for (int i = 0; i < jsonArray.length(); i++) {
try {
String tableString = jsonArray.getString(i);
saveATableRecord(tableString);
} catch (Exception ex) {
showLog("EXCEPTION A ");
}
}
}
});


} catch (Exception e) {
// showLog("EXCEPTION!!!! SAVETABLERECORD CLASS");
e.printStackTrace();
}
}

private void saveATableRecord(String tableData) {

showLog("Table name =" + className + " AND Data = " + tableData);

try {
Class cls = Class.forName("com.oper.max.model.db.sync_tables." + className);
Object obj = gson.fromJson(tableData, cls);
// realm.beginTransaction();
realm.copyToRealmOrUpdate((RealmObject) obj);
// realm.commitTransaction();
showLog("Object Added successfully");

} catch (Exception e) {
// realm.commitTransaction();
showLog("Exception in saveATableRecord = " + e.getMessage().toString());
e.printStackTrace();
}
}
}

最佳答案

您没有关闭您的 Realm 实例,您应该始终在非循环后台线程上关闭您的 Realm 实例。

改变这个:

private void initRecordSaving() {
try {
realm = Realm.getDefaultInstance();
showLog("-------------------InitRecordSaving()--------------------");
final JSONObject main = new JSONObject(json);
final JSONArray jsonArray = main.getJSONArray("records");
// String json = main.getString("records");
// saveATableRecord(json);

realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {

for (int i = 0; i < jsonArray.length(); i++) {
try {
String tableString = jsonArray.getString(i);
saveATableRecord(tableString);
} catch (Exception ex) {
showLog("EXCEPTION A ");
}
}

}
});


} catch (Exception e) {
// showLog("EXCEPTION!!!! SAVETABLERECORD CLASS");
e.printStackTrace();
}
}

private void saveATableRecord(String tableData) {

showLog("Table name =" + className + " AND Data = " + tableData);

try {
Class cls = Class.forName("com.oper.max.model.db.sync_tables." + className);
Object obj = gson.fromJson(tableData, cls);
// realm.beginTransaction();
realm.copyToRealmOrUpdate((RealmObject) obj);
// realm.commitTransaction();
showLog("Object Added successfully");

} catch (Exception e) {
// realm.commitTransaction();
showLog("Exception in saveATableRecord = " + e.getMessage().toString());
e.printStackTrace();
}

}

对此:

private void initRecordSaving() {
Realm realm = null;
try {
realm = Realm.getDefaultInstance();
showLog("-------------------InitRecordSaving()--------------------");
final JSONObject main = new JSONObject(json);
final JSONArray jsonArray = main.getJSONArray("records");
// String json = main.getString("records");
// saveATableRecord(json);

realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {

for (int i = 0; i < jsonArray.length(); i++) {
try {
String tableString = jsonArray.getString(i);
saveATableRecord(realm, tableString);
} catch (Exception ex) {
showLog("EXCEPTION A ");
}
}

}
});
} catch (Exception e) {
// showLog("EXCEPTION!!!! SAVETABLERECORD CLASS");
e.printStackTrace();
} finally {
if(realm != null) {
realm.close();
}
}
}

private void saveATableRecord(Realm realm, String tableData) {

showLog("Table name =" + className + " AND Data = " + tableData);

try {
Class cls = Class.forName("com.oper.max.model.db.sync_tables." + className);
Object obj = gson.fromJson(tableData, cls);
// realm.beginTransaction();
realm.copyToRealmOrUpdate((RealmObject) obj);
// realm.commitTransaction();
showLog("Object Added successfully");

} catch (Exception e) {
// realm.commitTransaction();
showLog("Exception in saveATableRecord = " + e.getMessage().toString());
e.printStackTrace();
}

}

关于Android Realm占用大量数据空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39696996/

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