Skip to content

Instantly share code, notes, and snippets.

@PondThaitay
Last active February 7, 2017 13:50
Show Gist options
  • Save PondThaitay/6511362160cdb7ef18e571be3ec26771 to your computer and use it in GitHub Desktop.
Save PondThaitay/6511362160cdb7ef18e571be3ec26771 to your computer and use it in GitHub Desktop.
public class StudentManager {
private final Realm realm;
private static StudentManager instance;
public StudentManager() {
realm = Realm.getDefaultInstance();
}
public static StudentManager getInstance() {
if (instance == null)
instance = new StudentManager();
return instance;
}
public Realm getRealm() {
return realm;
}
public void clearAll() {
realm.beginTransaction();
realm.delete(Student.class);
realm.commitTransaction();
}
public void deleteStudent(int id) {
realm.beginTransaction();
realm.where(Student.class)
.equalTo("studentId", id)
.findFirst()
.deleteFromRealm();
realm.commitTransaction();
}
public RealmResults<Student> getStudents() {
return realm.where(Student.class).findAll();
}
public RealmResults<Student> getStudents(int studentId) {
return realm.where(Student.class).equalTo("studentId", studentId).findAll();
}
public RealmResults<Student> queryedStudent(String firstName) {
return realm.where(Student.class)
.contains("firstName", firstName)
.findAll();
}
public void addStudent(Student student) {
realm.beginTransaction();
realm.insert(student);
realm.commitTransaction();
}
public void updateStudent(int id, String name) {
Student student = realm.where(Student.class)
.equalTo("studentId", id)
.findFirst();
realm.beginTransaction();
student.setFirstName(name);
realm.insertOrUpdate(student);
realm.commitTransaction();
}
public int nextId() {
Number student = realm.where(Student.class).max("studentId");
if (student == null)
return 10001;
else
return student.intValue() + 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment