Created
December 12, 2021 13:55
-
-
Save michzio/1757996b24af4bbd16b777d717826015 to your computer and use it in GitHub Desktop.
SwiftUI MVVM architecture - Dao
This file contains 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
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