WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Combine / UIViewController- Is it a good practice?

Forums > iOS

Hello,

I am using the Combine framework, and I have a question about my implementation. Is it a good practice to embed a view controller presentation in a Future and wait for this view controller to call back before publishing a value?

The following function will display a view controller (requester), wait for the user to fill the value, and send this value in the promise.

 private func requestDisplayName() -> Future<String, Never> {
        Future { [weak self] promise in
            guard let self = self else { return }
            let requester = RequestDisplayNameViewController()
            requester.displayName
                .dropFirst()
                .removeDuplicates()
                .receive(on: DispatchQueue.main)
                .sink { displayName in
                    promise(.success(displayName))
                }
                .store(in: &self.cancellables)
            self.mainVC.present(requester, animated: true, completion: nil)
        }
    }

displayName is a CurrentValueSubject<String, Never> that sends a value once the user hit a validate button in the RequestDisplayNameViewController

On the other side, I have an apiObserver waiting to receive the display name from the server, and once the data obtained, it will check if the value is nil or not, and according to this value, the requestDisplayName() will be called.

apiObserver.receive(on: DispatchQueue.main)
            .flatMap({ displayNameFromServer -> AnyPublisher<String, Never> in
                if let displayNameFromServer = displayNameFromServer {
                    return Result.Publisher(displayNameFromServer).eraseToAnyPublisher()
                }
                return self.requestDisplayName().eraseToAnyPublisher()
            })
            .flatMap({ displayName in
                // Make an action with the displayName
            })
            // ..

Thank you.

1      

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.