Question about Chapter 8 Challenge Part 1: share() publisher

let newPhotos = photos.selectedPhotos
  .filter { $0.size.width > $0.size.height }
  .prefix(while: { [unowned self] _ in
    return self.images.value.count < 6

  .map { [unowned self] newImage in
  // 1
    return self.images.value + [newImage]
  // 2
  .assign(to: \.value, on: images)
  // 3
  .store(in: &subscriptions)

  .delay(for: 2.0, scheduler: DispatchQueue.main)
  .sink(receiveCompletion: { [unowned self] _ in
    self.updateUI(photos: self.images.value)
  }, receiveValue: { _ in })
  .store(in: &subscriptions)

  .filter { [unowned self] _ in
    return self.images.value.count == 5
  .flatMap { [unowned self] _ in
    self.alert(title: "Reach limit", text: "You have reached your limit")
  .sink(receiveValue: { [unowned self] in
    self.navigationController?.popViewController(animated: true)
  .store(in: &subscriptions)

From the code above, newPhotos is a share publisher. There are 3 subscriptions afterwards. My questions is that is there a guarantee which subscriptions will be called first since the first subscription will increase the images value count which the 3rd subscription rely on that value to display alert.
How do we make sure one runs before another and keep the subscriptions separate

I think there is an issue with the code above. I am also able to reproduce the problem. (When selecting the 5th photo and alert is showing, probably due to 1st subscription (counter) gets triggered before the 3rd subscription (filter))

Hi, if you do separate subscriptions like in the example there is no real way to guarantee which one is going to be called first. If you want to guarantee the code execution order it’s better to use a single subscription and perform all the login in that subscription.

The book showcases various techniques and in this chapter the point was to create several subscriptions and use share() to achieve that. In that sense - the code is not optimized for efficiency but to walk you through learning as much as possible.