UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How upload data through Coordinator?

Forums > Swift

Good afternoon everyone. Guys, tell me how to update data through the coordinator. Somehow I can`t google the information. Thanks in advance for your help.

Link project

Example of my code.

Presentor

import Foundation
// 
typealias FirstSceneClosure = (Result<FirstModel.Responce.AnimalResponce, ResponceError>) -> Void
typealias Responce = FirstModel.Responce

// Presentation logic
protocol IFirstPresenter: AnyObject {
    /// Function transfers data from 'Iterator' in 'Presenter'
    /// - Parameters:
    ///   - responce: Accepts 'FirstModel.Responce'
    func present(responce: Responce)
}

final class FirstPresenter {

    // MARK: - Public properties
    var firstResultClosure: FirstSceneClosure?
    var testClosure: () -> Void

    // MARK: - Initializator
    internal init(
        firstSceneClosure: FirstSceneClosure?,
        testClosure: @escaping () -> Void
    ) {
        self.firstResultClosure = firstSceneClosure
        self.testClosure = testClosure
    }
}

extension FirstPresenter: IFirstPresenter {
    func present(responce: Responce) {
        firstResultClosure?(responce.responceResult)
        testClosure()
    }
}

Coorinator

import UIKit

protocol IFirstCoordinator: ICoordinator {
    func showFirstScene()
}

final class FirstCoordinator: IFirstCoordinator {

    var childCoordinators: [ICoordinator] = []

    var finishDelegate: ICoordinatorFinishDelegate?

    var navigateController: UINavigationController

    internal init(navigateController: UINavigationController) {
        self.navigateController = navigateController
    }

    func start() {
        showFirstScene()
    }

    func showFirstScene() {
        let assambly = FirstConfigurator()
        let viewController = FirstViewController()
        let firstVC = assambly.configure(
            viewController: viewController,
            firstSceneClosure: handlingResult
        )
        navigateController.pushViewController(firstVC, animated: true)
    }
}

private extension FirstCoordinator {
    private func handlingResult(
        result: Result<FirstModel.Responce.AnimalResponce, ResponceError>
    ) {
        let model: FirstModel.ViewModel
        switch result {
        case .success(let result):
            model = .success(FirstModel.ViewModel.AnimalViewModel(
                pet: result.pet,
                animal: result.name,
                image: result.image
            ))
        case .failure(let error):
            model = .error(error.errorDescription)
        }
        print("Data from Presenter \(model)")
        // How transwer in FirsrViewController?
    }
}

2      

Updating data through coordinators can be a complex task, and the information might not always be readily available online. However, I can help you with this.

To effectively guide you, I need some more context about your specific situation. Please tell me:

What kind of data are you trying to update? Is it in a database, a file system, or something else? What type of coordinator are you using? Is it a distributed coordinator like ZooKeeper or a centralized one like Spring Cloud Bus? What programming language or framework are you using? Knowing the specific tools involved will help me provide more relevant guidance. What have you tried so far? Are there any specific errors or challenges you're facing? Once I have this information, I can provide you with specific instructions on how to update data through the coordinator. I can also share relevant resources and best practices to help you succeed.

In the meantime, here are some general tips for updating data through coordinators:

Make sure you have the necessary permissions. Updating data through a coordinator can be a sensitive operation, so ensure you have the proper authorization. Use transactions whenever possible. Transactions help ensure data consistency even if the update process fails. Be mindful of potential conflicts. If multiple clients are trying to update the same data simultaneously, the coordinator might need to resolve conflicts. Test your updates thoroughly. Before deploying your updates to production, make sure they work as expected in a test environment.

2      

Good day, @Rohail22. Updating data througt the element Coorditator. As they explandet to me, this is not the fight idea. This is violates the principles of architecture and class responsibilites. 🙄

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.