Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RealmSwift
- enum SCResult<T> {
- case success(T)
- case failure(Error)
- var error: Error? {
- switch self {
- case .success(_):
- return nil
- case .failure(let error):
- return error
- }
- }
- }
- enum SCError: Error {
- case itemNotFound
- case noItemsAvailable
- var localizedDescription: String {
- switch self {
- case .itemNotFound:
- return "Item could not be found in the Realm."
- case .noItemsAvailable:
- return "No items belonging to the specified type could be found in the Realm."
- }
- }
- }
- struct SCPersistenceManager {
- // MARK: - Public Methods
- /// Adds or updates an existing object into the Realm.
- ///
- /// - Parameter item: Object to be added to default Realm. Must inherit from RealmSwift's `Object` class.
- /// - Returns: Whether the process completed successfully or failed.
- static func saveItem<T: Object>(item: T) -> SCResult<Void> {
- switch getRealm() {
- case .success(let realm):
- do {
- try realm.write {
- realm.add(item, update: T.primaryKey() != nil)
- }
- return .success(())
- } catch {
- return .failure(error)
- }
- case .failure(let error):
- return .failure(error)
- }
- }
- /// Creates a write transaction on the default Realm so any item inheriting from RealmSwift's `Object` class can be updated.
- ///
- /// - Parameter update: Block containing the updates to be perfomed.
- /// - Returns: Whether the process completed successfully or failed.
- static func updateItem(_ update:(() -> Void)) -> SCResult<Void> {
- switch getRealm() {
- case .success(let realm):
- do {
- try realm.write(update)
- return .success(())
- } catch {
- return .failure(error)
- }
- case .failure(let error):
- return .failure(error)
- }
- }
- /// Retrieves the single instance of a given object type with the given primary key from the default Realm.
- /// This method requires that primaryKey() be overridden on the given object class.
- ///
- /// - Parameters:
- /// - primaryKey: The primary key of the desired object.
- /// - type: The type of the object to be returned.
- /// - Returns: Whether the process completed successfully or failed. If successfull, requested item is appended to the response.
- static func getItem<T: Object>(primaryKey: String, type: T.Type) -> SCResult<T> {
- switch getRealm() {
- case .success(let realm):
- guard let item = realm.object(ofType: type, forPrimaryKey: primaryKey) else {
- return .failure(SCError.itemNotFound)
- }
- return .success(item)
- case .failure(let error):
- return .failure(error)
- }
- }
- /// Returns all objects of the given type stored in the default Realm.
- ///
- /// - Parameter type: The type of the objects to be returned.
- /// - Returns: Whether the process completed successfully or failed. If successfull, array of items of the requested type is appended to the response.
- static func getAllItems<T: Object>(type: T.Type) -> SCResult<[T]> {
- switch getRealm() {
- case .success(let realm):
- let items = Array(realm.objects(type))
- return !items.isEmpty ? .success(items) : .failure(SCError.noItemsAvailable)
- case .failure(let error):
- return .failure(error)
- }
- }
- /// Deletes an object from the default Realm. Once the object is deleted it is considered invalidated
- ///
- /// - Parameter item: Object to be deleted.
- /// - Returns: Whether the process completed successfully or failed.
- static func deleteItem<T: Object>(item: T) -> SCResult<Void> {
- switch getRealm() {
- case .success(let realm):
- do {
- try realm.write {
- realm.delete(item)
- }
- return .success(())
- } catch {
- return .failure(error)
- }
- case .failure(let error):
- return .failure(error)
- }
- }
- /// Deletes all objects from the Realm.
- ///
- /// - Returns: Whether the process completed successfully or failed.
- static func deleteAllItems() -> SCResult<Void> {
- switch getRealm() {
- case .success(let realm):
- do {
- try realm.write {
- realm.deleteAll()
- }
- return .success(())
- } catch {
- return .failure(error)
- }
- case .failure(let error):
- return .failure(error)
- }
- }
- // MARK: - Private Methods
- static private func getRealm() -> SCResult<Realm> {
- do {
- let realm = try Realm()
- return .success(realm)
- } catch {
- print("Failed to initialize Realm. Failed with error: \(error.localizedDescription)")
- return .failure(error)
- }
- }
- }
Add Comment
Please, Sign In to add comment