问题
🙏🏻
How at this example send dynamic link to service class any time and after this update view I received link any time and I dont want to use notification and etc
View
var viewModel = HomeViewModel() func configureViewModel() { viewModel.resultClosure = { [weak self] listModel in guard let image = listModel.uiImage else { return } self?.updateImageViewConstraint(nil, image: image) } }
HomveViewModel
class HomeViewModel { var service = Services() var listModel = [ListModel]() { didSet { DispatchQueue.main.async { [weak self] in guard let self = self else { return } self.resultClosure?(self.listModel[0]) } } } let placeholderImage = UIImage(named: "placeholder") var updatedImge : UIImage? var resultClosure: ((ListModel) -> Void)? var stringUrl: StringUrl! func fetchData() { service.makeRequest() { (result: Result<[ListModel]>) in switch result { case .success(let listModel): self.listModel = listModel return case .failure(let error): print("\n HomeViewModel fetchData failure \(error) \n") return } } }
}
Services - Here I need to check the dynamic link
class Services { let urlSession: URLSession let imageProvider = ImageProvider() let dynamicLinkfromStruct = "Dynamic Link From Struct!!!" init(urlSession: URLSession = .shared) { self.urlSession = urlSession } func makeRequest(completionHandler: @escaping (Result<[ListModel]>) -> Void) { guard let url = URL(string: dynamicLinkfromStruct) else { return } imageProvider.loadImages(from: url) { (image) in do { let result = [ListModel.init(imageUrl: self.url, uiImage: image)] completionHandler(.success(result)) } catch { completionHandler(.failure(error )) } } } }
Model
struct ListModel { let imageUrl: String? let uiImage: UIImage? }
Struct where I catch any time dynamic links which I need to show at View. Question here! How to Send Link to Services and update View???
struct GetManager { static func showContent(pasteboard: String) { switch onlyLinkAbsoluteString.validURL { case true: // How to Send Link to Services and update View??? } } }
回答1:
In MVVM/MVC communication should take this path
View > ViewModel > Manager > Service
Create an instance of your Manager in your ViewModel and an Instance of your Service in your Manager
You call the Manager's function in your ViewModel and the Manager would handle the communication with your Service.
来源:https://stackoverflow.com/questions/65280894/dynamic-link-at-mvc-model