Skip to content

Instantly share code, notes, and snippets.

@michzio
Created December 12, 2021 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michzio/1757996b24af4bbd16b777d717826015 to your computer and use it in GitHub Desktop.
Save michzio/1757996b24af4bbd16b777d717826015 to your computer and use it in GitHub Desktop.
SwiftUI MVVM architecture - Dao
class WordsetCategoryDao: BaseDao<WordsetCategoryDTO, WordsetCategory>, WordsetCategoryDaoProtocol {
func update(_ entities: [WordsetCategoryDTO]) -> Future<UpdateResult, Error> {
Future { [weak self] promise in
let context = self?.storage.backgroundContext
context.perform {
switch Self.update(context: context, entities) {
case .success(let change):
self?.storage.saveContext(context)
promise(.success(change))
case .failure(let error):
promise(.failure(error))
}
}
}
}
// MARK: - Mapping DTO to managed objects
override func encode(entity e: WordsetCategoryDTO, into obj: inout WordsetCategory) {
obj.categoryId = String(e.categoryId)
obj.foreignName = e.foreignName
obj.nativeName = e.nativeName
}
override func decode(object o: WordsetCategory) -> WordsetCategoryDTO {
WordsetCategoryDTO(categoryId: o.id, foreignName: o.foreignName!, nativeName: o.nativeName!, image: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment