Dynamic Link at MVC model

Deadly 提交于 2021-01-01 08:44:34

问题


🙏🏻

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

  1. View

    var viewModel = HomeViewModel()
    
     func configureViewModel() {
         viewModel.resultClosure = { [weak self] listModel in
             guard let image = listModel.uiImage else { return }
             self?.updateImageViewConstraint(nil, image: image)
         }
     }
    
  2. 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
             }
         }
     }
    

    }

  3. 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 ))
             }
         }
     }
    }
    
  4. Model

    struct ListModel {
    
     let imageUrl: String?
     let uiImage: UIImage?
    }
    
  5. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!