如果您在Playgroud中尝试以下代码:

import Combine
import Foundation

struct User {
    let name: String
}

private var subscriptions = Set<AnyCancellable>()
var didAlreadyImportUsers = false
var users = [User]()

func importUsers() -> Future<Bool, Never> {
    Future { promise in
        DispatchQueue.global(qos: .userInitiated).async {
            sleep(5)
            users = [User(name: "John"), User(name: "Jack")]
            promise(.success(true))
        }
    }
}

func getUsers(age: Int? = nil) ->Future<[User], Error> {
    Future { promise in
        promise(.success(users))
    }
}

var usersPublisher: AnyPublisher<[User], Error> {
    if didAlreadyImportUsers {
        return getUsers().eraseToAnyPublisher()
    } else {
        return importUsers()
            .setFailureType(to: Error.self)
            .combineLatest(getUsers())
            .map { $0.1 }
            .eraseToAnyPublisher()
    }
}

usersPublisher
    .sink(receiveCompletion: { completion in
    print(completion)
}, receiveValue: { value in
    print(value)
}).store(in: &subscriptions)

它将打印:
[]
finished

但我期望:
[User(name: "John"), User(name: "Jack")]
finished


如果我删除了带有sleep(5)的行,那么它将正确打印结果。似乎是异步问题。好像.combineLatest(getUsers())不在等待importUsers()了,我以为combineLatest正在解决这个问题?我在这里想念什么?

(在我的真实代码中,有一个长期运行的Core Data操作而不是sleep)

最佳答案

如您正确预期的那样,CombineLatest会等待,但是在您的情况下getUsers已经准备好一个值,即[];即users运行时是什么getUsers

在发生一些异步操作之前,您实际上不需要使用CombineLatest来“等待”。您可以链式发布者:

return importUsers()
           .setFailureType(to: Error.self)
           .flatMap { _ in
               getUsers()
           }
           .eraseToAnyPublisher()

实际上,如果您可以假设getUsersusers之后填充,甚至不需要importUsers:
return importUsers()
           .map { _ in
               self.users
           }
           .eraseToAnyPublisher()

10-06 10:31