如何从Vapor的阵列中获得“未来”?

时间:2019-12-14 15:03:11

标签: swift vapor

具有以下代码:

router.get("/fetchOngoingReleases", String.parameter) { (request) -> Future<[ReleaseWithUser]> in
    return Release.query(on: request).filter(\.inProgress == true).all().flatMap { (ra) -> EventLoopFuture<[ReleaseWithUser]> in
        let userId = try request.parameters.next(String.self)
        return User.query(on: request).filter(\.fbId == userId).first().flatMap { (user) -> EventLoopFuture<[ReleaseWithUser]> in
            let a = ra.map { (r) -> ReleaseWithUser in
                // some condition and logic here
            }

            return a. //need this to make future
        }
    }
}

我检查了所有可能的方法调用,但没有将[ReleaseWithUser]转换为Future<[ReleaseWithUser]>。你有什么主意吗?

1 个答案:

答案 0 :(得分:1)

解决方案是:

class Data: ObservableObject {

    @Published var dataToUpdate: Int 
}

struct UpdateView : View {

@EnvironmentObject data: Data


   ...body ... {
     // use data.dataToUpdate here
  }
}

可以使用ra.map { (r) -> ReleaseWithUser in // some condition and logic here }.flatten(on: request) 将数组转换为将来的数组。