Sunday, September 25, 2022
HomeTechnologyios - Turning function that has a combination of publishers/sinks into an...

ios – Turning function that has a combination of publishers/sinks into an async function


I have the following function loadData and I want to use it within refreshable of a SwiftUI list. For this I need to make it an async function:

func loadData() {
  // Publishers
  let followersPublisher = modelLoader.loadAllFollowers(withId: id)
  let followingPublisher = modelLoader.loadAllFollowing(withId: id)
  let friendshipsPublisher = Publishers.Zip(followersPublisher, followingPublisher)
    .share()
    .eraseToAnyPublisher()

  // Sinks
  getFollowers(from: followersPublisher)
  getFollowerChange(
    from: followersPublisher,
    cachedFollowers: followers
  )
  getFollowing(from: followingPublisher)
  getNotFollowingUserBack(from: friendshipsPublisher)
  getUserNotFollowing(from: friendshipsPublisher)

  followersPublisher
    .connect()
    .store(in: &cancellables)
  followingPublisher
    .connect()
    .store(in: &cancellables)
}

Within this function, all the separate functions use Publisher sinks. For example:

private func getFollowing(from publisher: Publishers.MakeConnectable<AnyPublisher<Set<User>, Never>>) {
  publisher
    .sink(
      receiveCompletion: { _ in },
      receiveValue: { [weak self] following in
        self?.following = following
      }
    )
    .store(in: &cancellables)
}

How can I turn it into an async function so I can use await with it?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments