Hello,
This is how i implemented realm in my project,
`
public void addDealCategory(DealCategory dealCategory){
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.copyToRealmOrUpdate(dealCategory);
realm.commitTransaction();
realm.close();
}
public void addDealCategories(List<DealCategory> dealCategories){
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.copyToRealmOrUpdate(dealCategories);
realm.commitTransaction();
realm.close();
}
public void updateDealCategory(DealCategory dealCategory){
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.copyToRealmOrUpdate(dealCategory);
realm.commitTransaction();
realm.close();
}
public ArrayList<DealCategory> getAllDealCategory() {
ArrayList<DealCategory> result = new ArrayList<>();
Realm realm = Realm.getDefaultInstance();
RealmResults<DealCategory> dealCategories = realm.where(DealCategory.class)
.findAllSorted(DealCategory.COLUMN_CATEGORY_NAME);
for (DealCategory category : dealCategories){
DealCategory dealCategory = new DealCategory();
dealCategory.setCatId(category.getCatId());
dealCategory.setCatName(category.getCatName());
dealCategory.setCatSfid(category.getCatSfid());
dealCategory.setCatStamp(category.getCatStamp());
result.add(dealCategory);
}
realm.close();
return result;
}
public int getTableSize(){
Realm realm = Realm.getDefaultInstance();
RealmResults<DealCategory> dealCategories = realm.where(DealCategory.class)
.findAll();
int size = dealCategories.size();
realm.close();
return size;
}
public String getDealCategoryNameBySfId(String cat) {
Realm realm = Realm.getDefaultInstance();
DealCategory category = realm.where(DealCategory.class)
.equalTo(DealCategory.COLUMN_CATEGORY_SFID, cat)
.findFirst();
String categoryName = category.getCatName();
realm.close();
return categoryName;
}
public DealCategory getDealCategoryByName(String name) {
Realm realm = Realm.getDefaultInstance();
DealCategory realmResult = realm.where(DealCategory.class)
.equalTo(DealCategory.COLUMN_CATEGORY_NAME, name)
.findFirst();
if(realmResult != null) {
DealCategory result = new DealCategory();
result.setCatStamp(realmResult.getCatStamp());
result.setCatSfid(realmResult.getCatSfid());
result.setCatName(realmResult.getCatName());
result.setCatId(realmResult.getCatId());
realm.close();
return result;
}
realm.close();
return null;
}
public String getDealCategorySfIdByName(String categoryName) {
Realm realm = Realm.getDefaultInstance();
DealCategory category = realm.where(DealCategory.class)
.equalTo(DealCategory.COLUMN_CATEGORY_NAME, categoryName)
.findFirst();
String categorySfId;
if (category != null) {
categorySfId = category.getCatSfid();
} else {
categorySfId = "null";
}
realm.close();
return categorySfId;
}
public void deleteCategoryByName(String categoryName){
Realm realm = Realm.getDefaultInstance();
DealCategory category = realm.where(DealCategory.class)
.equalTo(DealCategory.COLUMN_CATEGORY_NAME, categoryName)
.findFirst();
realm.beginTransaction();
category.deleteFromRealm();
realm.commitTransaction();
realm.close();
}
public void deleteCategoryByWebId(long catId){
Realm realm = Realm.getDefaultInstance();
DealCategory category = realm.where(DealCategory.class)
.equalTo(DealCategory.COLUMN_WEB_ID,catId)
.findFirst();
realm.beginTransaction();
category.deleteFromRealm();
realm.commitTransaction();
realm.close();
}
public void deleteCategoryBySfId(String sfId){
Realm realm = Realm.getDefaultInstance();
DealCategory category = realm.where(DealCategory.class)
.equalTo(DealCategory.COLUMN_CATEGORY_SFID,sfId)
.findFirst();
realm.beginTransaction();
category.deleteFromRealm();
realm.commitTransaction();
realm.close();
}
public void clearDealCategories(){
Realm realm = Realm.getDefaultInstance();
RealmResults<DealCategory> allDealCategory = realm.where(DealCategory.class)
.findAll();
realm.beginTransaction();
allDealCategory.deleteAllFromRealm();
realm.commitTransaction();
realm.close();
}
`
After my transaction i always close the realm instance to make sure that data will not be edited while other function is reading it vice versa and retain old version of the data. But still, my data size is still increasing.
Hope anyone can help me,
Thanks in advance.
You're probably using addDealCategory in a for loop and therefore adding about 1000 categories with 1000 transactions.
Also, unless you have a random open Realm somewhere else that you haven't closed, getDealCategoryByName wouldn't even work because you're invalidating its Realm instance.
Consider referring to the documentation for when to close Realm instances.
https://realm.io/docs/java/latest/#controlling-the-lifecycle-of-realm-instances
(although for fragments it should be onCreateView/onDestroyView)
I'm now using addDealCategories so i don't have to loop anymore to add categories. And I'm using async task in serial to make sure one realm instance is only opened.
@MichaelReyes The size will never decrease - Realm will reuse memory but will never free it. That implies that deleting an object, the space used by the object will eventually be used by other object. If you need to reduce the file size, you can compact the Realm file when your app starts.
@kneth this helps. but if it can only be done during the app opens, large file size issue may still affect the app when the user use it continuously. But still, big thanks man.
@MichaelReyes Currently Realm doesn't offer online compaction. It is related to https://github.com/realm/realm-core/issues/2343
Personally I prefer to attempt to compact the Realm when the app is closing (there were 0+ activities, but now there are only 0 activities), merely from a start-up time UX standpoint.
@kneth , @Zhuinden I'm now closing the issue. Thanks for you help. I really appreciate it.