Created
April 12, 2022 21:26
-
-
Save ARamy23/12338563410ef31207fe1b36b2eb28a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | |
} | |
} | |
} | |
} |
Author
ARamy23
commented
Apr 12, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment