Guest User

Untitled

a guest
Feb 22nd, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. import RealmSwift
  2.  
  3. enum SCResult<T> {
  4. case success(T)
  5. case failure(Error)
  6.  
  7. var error: Error? {
  8. switch self {
  9. case .success(_):
  10. return nil
  11. case .failure(let error):
  12. return error
  13. }
  14. }
  15. }
  16.  
  17. enum SCError: Error {
  18. case itemNotFound
  19. case noItemsAvailable
  20.  
  21. var localizedDescription: String {
  22. switch self {
  23. case .itemNotFound:
  24. return "Item could not be found in the Realm."
  25. case .noItemsAvailable:
  26. return "No items belonging to the specified type could be found in the Realm."
  27. }
  28. }
  29. }
  30.  
  31. struct SCPersistenceManager {
  32. // MARK: - Public Methods
  33.  
  34. /// Adds or updates an existing object into the Realm.
  35. ///
  36. /// - Parameter item: Object to be added to default Realm. Must inherit from RealmSwift's `Object` class.
  37. /// - Returns: Whether the process completed successfully or failed.
  38. static func saveItem<T: Object>(item: T) -> SCResult<Void> {
  39. switch getRealm() {
  40. case .success(let realm):
  41. do {
  42. try realm.write {
  43. realm.add(item, update: T.primaryKey() != nil)
  44. }
  45. return .success(())
  46. } catch {
  47. return .failure(error)
  48. }
  49. case .failure(let error):
  50. return .failure(error)
  51. }
  52. }
  53.  
  54. /// Creates a write transaction on the default Realm so any item inheriting from RealmSwift's `Object` class can be updated.
  55. ///
  56. /// - Parameter update: Block containing the updates to be perfomed.
  57. /// - Returns: Whether the process completed successfully or failed.
  58. static func updateItem(_ update:(() -> Void)) -> SCResult<Void> {
  59. switch getRealm() {
  60. case .success(let realm):
  61. do {
  62. try realm.write(update)
  63. return .success(())
  64. } catch {
  65. return .failure(error)
  66. }
  67. case .failure(let error):
  68. return .failure(error)
  69. }
  70. }
  71.  
  72. /// Retrieves the single instance of a given object type with the given primary key from the default Realm.
  73. /// This method requires that primaryKey() be overridden on the given object class.
  74. ///
  75. /// - Parameters:
  76. /// - primaryKey: The primary key of the desired object.
  77. /// - type: The type of the object to be returned.
  78. /// - Returns: Whether the process completed successfully or failed. If successfull, requested item is appended to the response.
  79. static func getItem<T: Object>(primaryKey: String, type: T.Type) -> SCResult<T> {
  80. switch getRealm() {
  81. case .success(let realm):
  82. guard let item = realm.object(ofType: type, forPrimaryKey: primaryKey) else {
  83. return .failure(SCError.itemNotFound)
  84. }
  85. return .success(item)
  86. case .failure(let error):
  87. return .failure(error)
  88. }
  89. }
  90.  
  91. /// Returns all objects of the given type stored in the default Realm.
  92. ///
  93. /// - Parameter type: The type of the objects to be returned.
  94. /// - Returns: Whether the process completed successfully or failed. If successfull, array of items of the requested type is appended to the response.
  95. static func getAllItems<T: Object>(type: T.Type) -> SCResult<[T]> {
  96. switch getRealm() {
  97. case .success(let realm):
  98. let items = Array(realm.objects(type))
  99. return !items.isEmpty ? .success(items) : .failure(SCError.noItemsAvailable)
  100. case .failure(let error):
  101. return .failure(error)
  102. }
  103. }
  104.  
  105. /// Deletes an object from the default Realm. Once the object is deleted it is considered invalidated
  106. ///
  107. /// - Parameter item: Object to be deleted.
  108. /// - Returns: Whether the process completed successfully or failed.
  109. static func deleteItem<T: Object>(item: T) -> SCResult<Void> {
  110. switch getRealm() {
  111. case .success(let realm):
  112. do {
  113. try realm.write {
  114. realm.delete(item)
  115. }
  116. return .success(())
  117. } catch {
  118. return .failure(error)
  119. }
  120. case .failure(let error):
  121. return .failure(error)
  122. }
  123. }
  124.  
  125. /// Deletes all objects from the Realm.
  126. ///
  127. /// - Returns: Whether the process completed successfully or failed.
  128. static func deleteAllItems() -> SCResult<Void> {
  129. switch getRealm() {
  130. case .success(let realm):
  131. do {
  132. try realm.write {
  133. realm.deleteAll()
  134. }
  135. return .success(())
  136. } catch {
  137. return .failure(error)
  138. }
  139. case .failure(let error):
  140. return .failure(error)
  141. }
  142. }
  143.  
  144. // MARK: - Private Methods
  145.  
  146. static private func getRealm() -> SCResult<Realm> {
  147. do {
  148. let realm = try Realm()
  149. return .success(realm)
  150. } catch {
  151. print("Failed to initialize Realm. Failed with error: \(error.localizedDescription)")
  152. return .failure(error)
  153. }
  154. }
  155. }
Add Comment
Please, Sign In to add comment