Skip to content

Instantly share code, notes, and snippets.

@ARamy23
Created April 12, 2022 21:26
Show Gist options
  • Save ARamy23/12338563410ef31207fe1b36b2eb28a1 to your computer and use it in GitHub Desktop.
Save ARamy23/12338563410ef31207fe1b36b2eb28a1 to your computer and use it in GitHub Desktop.
import Foundation
class Model {
let id: String
let link: String
let resizedLink: String
func toDao() -> ModelDAO {
let dao: ModelDAO = .init()
// properties injection
return dao
}
}
class ModelDAO {
@Persisted(primaryKey: true) var id: String
@Persisted var link: String
@Persisted var resizedLink: String
func toModel() -> Model {
return Model(id, link, resized)
}
}
class Network {
static func callAPI(completionHandler: @escaping (Result<Model, Error>) -> Void) {
// ...
}
}
class Database {
static func saveModelDAO(_ model: Model) {
realm.write {
realm.add(model.toDao(), update: .all)
}
}
static func getModelDAO() {
realm.objects(ModelDAO.self).map { $0.toModel }
}
}
class Repository {
static func homePhotos(_ completion: @escaping (Result<[String], Error>) -> ()) {
completion(.success(Database.getModelDAO()))
Network.callAPI(completionHandler: {
switch $0 {
case let .success(model):
Database.saveModelDAO(model)
completion(Database.getModelDAO())
case .failure: break
}
})
}
}
class ViewController {
init() {
Repository.homePhotos { results in
switch {
case let .success(photos):
}
}
}
}
@ARamy23
Copy link
Author

ARamy23 commented Apr 12, 2022

CleanShot 2022-04-12 at 23 26 38@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment