Home > AI > IOS > Combine >

Deferred

Example :

class Demo14 {
    var cancellables = Set<AnyCancellable>()

    let pub = Deferred<AnyPublisher<Bool, Error>> {
           print(Date(), "Future inside Deferred created")
        
           return Future<Bool, Error> { promise in
               promise(.success(true))
           }.eraseToAnyPublisher()
        
       }.eraseToAnyPublisher()
    
    
    func example(of: String) {
        pub.sink(receiveCompletion: {
            print("Received completion:   \($0)")
        }, receiveValue:  {value in
            print("Received value:   \(value)")
        })
        .store(in: &cancellables)
    }
}


let d = Demo14()
d.example(of: "Good")

Leave a Reply