PromiseKit 6.0迁移

时间:2018-04-13 07:50:04

标签: ios swift promise promisekit

我决定在我的项目中将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()))的一行中,我收到了错误消息:

  

参数标签&#39;(价值:)&#39;不匹配任何可用的重载

根据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()))
    }
}

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

  

无法转换类型&#39; Promise&lt; _。T&gt;&#39;的返回表达式返回类型&#39; Promise&lt; [JSON]&gt;&#39;

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

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()))不起作用?

1 个答案:

答案 0 :(得分:0)

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

  

map被提供前一个承诺值并要求您返回一个非承诺,即。价值。

所以:

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更好。简单又快捷。