Saturday, November 21, 2026
HomeiOS Developmentios - Mix Subscriber not receiving values instantly

ios – Mix Subscriber not receiving values instantly

[ad_1]

I’m making an attempt to give you a Mix pipeline that does the next

  • Make an API name with sure pageIndex and ship the outcomes to subscriber
  • Increment the web page Index and repeat the above till backend says there’s nothing extra to fetch

Here’s what I’ve finished up to now

struct APIResponse {
  var hasNextPage: Bool = false
  var nextPageIndex: Int = -1
}
class Tester {
    func getAllDetails() -> AnyPublisher<APIResponse, By no means> {
        // Topic holding the web page index to be fetched
        let topic = CurrentValueSubject<Int, By no means>(0)
        
        return topic.flatMap ({ index in
            return self.getDetails(index: index)
        })
        .handleEvents(receiveOutput: { response in
            if response.hasNextPage {
                topic.ship(response.nextPageIndex)
            } else {
                topic.ship(completion: .completed)
            }
        })
 // Ignore the decision, Simply did it please the compiler
        .replaceError(with: APIResponse())
        .eraseToAnyPublisher()
    }
    
    func getDetails(index: Int) -> AnyPublisher<APIResponse,MockError> {
        Future { promise in
            // Mocking API Response right here
            if index < 5 {
                promise(.success(APIResponse(hasNextPage: true, nextPageIndex: index+1)))
            } else if index == 5 {
                promise(.success(APIResponse(hasNextPage: false)))
            } else {
                promise(.failure(MockError()))
            }
        }
        .eraseToAnyPublisher()
    }
}

let tester =  Tester()
tester.getAllDetails()
    .sink { _ in
        print("accomplished")
    } receiveValue: { numbers in
        print(numbers.debugDescription)
    }

The pipeline is working nevertheless it delivering all the outcomes to subscriber on the finish and never as they arrive. How do I modify this to let subscriber obtain intermediate values

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments