2017-05-06 87 views
2

是否有辦法獲取Realm中的所有現有對象?獲取Android中的所有領域對象

有大約1000個對象(表),每個對象有10000+項。 每1小時有一些項目已過期。

// Class[] clazzez = realm.getSchema(); 

Class[] clazzez = new Class[] { // How to get all classes dynamically? 
    Demo.class, AnotherDemo.class, SomeOtherDemo.class 
}; 


// To delete expired items 
for(Class clazz: clazzez) { 
    realm.where(clazz) 
      .greaterThan("maxAge", new Date().getTime()) 
      .findAll() 
      .deleteAllFromRealm(); 
} 

跟蹤所有類別非常困難。如何動態獲取所有類?

回答

3

With realmConfiguration.getRealmObjectClasses();(0.88.0+)。

RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()...build(); 
// ... 
Set<Class<? extends RealmModel>> schemaClasses = realmConfiguration.getRealmObjectClasses(); // <--- 

// To delete expired items 
for(Class clazz: schemaClasses) { 
    realm.where(clazz) 
      .greaterThan("maxAge", new Date().getTime()) // <-- assumes existence of "maxAge" field 
      .findAll() 
      .deleteAllFromRealm(); 
}