DBManager.swift 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // DBManager.swift
  3. // Learn Genie
  4. //
  5. // Created by Suraj Kumar Mandal on 31/08/21.
  6. //
  7. import Foundation
  8. import RealmSwift
  9. class DBManager {
  10. public var database:Realm
  11. static let sharedInstance = DBManager()
  12. private init() {
  13. database = try! Realm()
  14. }
  15. //
  16. // func getDataFromDB(objs: [Object]) -> Results<Object> {
  17. //
  18. // let results: Results<Object> = database.objects(objs.self)
  19. // return results
  20. // }
  21. func getObjects(type: Object.Type) -> Results<Object>? {
  22. //return database.objects(type)
  23. let results: Results<Object> = database.objects(type)
  24. print("Get Objects")
  25. return results
  26. }
  27. func addData(objs: [Object]) {
  28. try! database.write {
  29. database.add(objs,update: .modified)
  30. print("Added new object")
  31. }
  32. }
  33. func deleteAllDatabase() {
  34. try! database.write {
  35. database.deleteAll()
  36. // database.refresh();
  37. print("Delete all database")
  38. }
  39. }
  40. func deleteFromDb(object: [Object]) {
  41. try! database.write {
  42. database.delete(object)
  43. print("Delete from database")
  44. }
  45. }
  46. }