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?