Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
UniversityRepositry
public class UniversityRepository implements IUniversityRepository {
@Override
public void addUniversity(University university, OnAddUniversityCallback callback) {
Realm realm = Realm.getInstance(SimpleRealmApp.getInstance());
realm.beginTransaction();
University u = realm.createObject(University.class);
u.setId(UUID.randomUUID().toString());
u.setName(university.getName());
realm.commitTransaction();
if (callback != null)
callback.onSuccess();
}
@Override
public void deleteUniversityById(String Id, OnDeleteUniversityCallback callback) {
Realm realm = Realm.getInstance(SimpleRealmApp.getInstance());
realm.beginTransaction();
University university = realm.where(University.class).equalTo(RealmTable.ID, Id).findFirst();
university.removeFromRealm();
realm.commitTransaction();
if (callback != null)
callback.onSuccess();
}
@Override
public void deleteUniversityByPosition(int position, OnDeleteUniversityCallback callback) {
Realm realm = Realm.getInstance(SimpleRealmApp.getInstance());
realm.beginTransaction();
RealmQuery query = realm.where(University.class);
RealmResults results = query.findAll();
results.remove(position);
realm.commitTransaction();
if (callback != null)
callback.onSuccess();
}
@Override
public void getUniversityById(String id, OnGetUniversityByIdCallback callback) {
Realm realm = Realm.getInstance(SimpleRealmApp.getInstance());
University result = realm.where(University.class).equalTo(RealmTable.ID, id).findFirst();
if (callback != null)
callback.onSuccess(result);
}
@Override
public void getAllUniversities(OnGetAllUniversityCallback callback) {
Realm realm = Realm.getInstance(SimpleRealmApp.getInstance());
RealmQuery query = realm.where(University.class);
RealmResults results = query.findAll();
if (callback != null)
callback.onSuccess(results);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment