我目前正在使用Realm进行CRM Android应用程序以保存记录。 我正在同步来自Web服务器的数据。有10个表。这一个表有大约450页,这样每个页面包含100条记录(100行的数据) 获取10个表的记录后,我的应用程序的数据大小已增长到 118MB 。
Realm的这个空间是否正常? ---> 118MB。
或者我对领域做错了什么? 我检查了我的记录是否有重复。没有重复,每行只保存一次。
这就是我在我的系统中配置Realm的方式 申请类
realmConfig = new RealmConfiguration.Builder(this).build();
Realm.setDefaultConfiguration(realmConfig);
后来我在任何需要的地方使用Default实例。并在应用程序类的onTerminate上关闭Realm
更多信息: Android工作室版本= 2.1.3
领域版本= 1.2.0
Device = Nexus 5 Marshmallow
我的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();
}
}
}
答案 0 :(得分:2)
您没有关闭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();
}
}