gpt4 book ai didi

ios - PromiseKit 6.0 迁移

转载 作者:行者123 更新时间:2023-11-28 07:50:32 25 4
gpt4 key购买 nike

我决定在我的项目中将 PromiseKit pod 从 4.5.x 升级到 6.x.x。在迁移过程中,我遇到了一个问题,我不能 100% 确定我是否以正确的方式解决了它。

看看我在 PromiseKit 4.5 中的示例代码:

func fetchSettings(forAnimalsGuids animalsGuids: [String]) -> Promise<[JSON]> {
if animalsGuids.isEmpty {
return .value([])
}

let packageSize = 100
let animalsPackages = animalsGuids.chunked(chunkSize: packageSize)

return when(fulfilled: animalsPackages.map({ animalsPackage -> Promise<[JSON]> in
let predicates: [GatewayRequestFactoryHelper.Predicate] = animalsPackage.map({ .atomic(.equal("Animal.ObjectGuid", .string($0))) })
let predicate: GatewayRequestFactoryHelper.Predicate? = !predicates.isEmpty ? .or(predicates) : nil

return gatewayConnector.fetchObjects(type: AnimalMilkSetting.self, predicate: predicate)
}))
.then { result in
Promise(value: Array(result.joined()))
}
}

Promise(value: Array(result.joined())) 的一行中我得到了一个错误:

Argument labels '(value:)' do not match any available overloads

根据 PromiseKit 迁移指南,return Promise(value: foo) 已被替换为:return .value(foo),所以我重构了我的代码:

func fetchAnimal(forAnimalsGuids animalsGuids: [String]) -> Promise<[JSON]> {
if animalsGuids.isEmpty {
return .value([])
}

let packageSize = 100
let animalsPackages = animalsGuids.chunked(chunkSize: packageSize)

return when(fulfilled: animalsPackages.map({ animalsPackage -> Promise<[JSON]> in
let predicates: [GatewayRequestFactoryHelper.Predicate] = animalsPackage.map({ .atomic(.equal("Animal.ObjectGuid", .string($0))) })
let predicate: GatewayRequestFactoryHelper.Predicate? = !predicates.isEmpty ? .or(predicates) : nil

return gatewayConnector.fetchObjects(type: AnimalMilkSetting.self, predicate: predicate)
}))
.then { result in
.value(Array(result.joined()))
// Promise(value: Array(result.joined()))
}
}

它没有帮助。编译显示错误信息:

Cannot convert return expression of type 'Promise<_.T>' to return type 'Promise<[JSON]>'

我做了一些调整,最终找到了一个被我的编译器接受的解决方案:

func fetchAnimal(forAnimalsGuids animalsGuids: [String]) -> Promise<[JSON]> {
if animalsGuids.isEmpty {
return .value([])
}

let packageSize = 100
let animalsPackages = animalsGuids.chunked(chunkSize: packageSize)

return when(fulfilled: animalsPackages.map({ animalsPackage -> Promise<[JSON]> in
let predicates: [GatewayRequestFactoryHelper.Predicate] = animalsPackage.map({ .atomic(.equal("Animal.ObjectGuid", .string($0))) })
let predicate: GatewayRequestFactoryHelper.Predicate? = !predicates.isEmpty ? .or(predicates) : nil

return gatewayConnector.fetchObjects(type: AnimalMilkSetting.self, predicate: predicate)
}))
.then { result in
Promise { $0.fulfill(Array(result.joined())) }
}
}

你能给我一个答案吗?我是否以正确的方式迁移了它?方法 .value(Array(result.joined())) 不起作用?

最佳答案

我想我自己找到了答案。我使用了 PromiseKit 6 中引入的 map。来自文档:

map is fed the previous promise value and requires you return a non-promise, ie. a value.

所以:

func fetchAnimal(forAnimalsGuids animalsGuids: [String]) -> Promise<[JSON]> {
if animalsGuids.isEmpty {
return .value([])
}

let packageSize = 100
let animalsPackages = animalsGuids.chunked(chunkSize: packageSize)

return when(fulfilled: animalsPackages.map({ animalsPackage -> Promise<[JSON]> in
let predicates: [GatewayRequestFactoryHelper.Predicate] = animalsPackage.map({ .atomic(.equal("Animal.ObjectGuid", .string($0))) })
let predicate: GatewayRequestFactoryHelper.Predicate? = !predicates.isEmpty ? .or(predicates) : nil

return gatewayConnector.fetchObjects(type: AnimalMilkSetting.self, predicate: predicate)
}))
.map {
Array($0.joined())
}
}

我认为这个解决方案比将结果包装在另一个 Promise 中要好。简单快捷。

关于ios - PromiseKit 6.0 迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49811888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com