0

我正在测试 Kentico Cloud Swift SDK 以返回一些“文章”内容类型(我创建了其中两个并已发布)。

我正在使用此处描述的样板代码:

我得到的结果是:[Kentico Cloud] Getting items action has succeeded. Received nil items.

我的代码:

let client = DeliveryClient.init(projectId: <project id>, previewApiKey: <preview key>, secureApiKey: <secure key>, enableDebugLogging: true)

func getArticles(){

    // Note: Using "items" as custom query returns all content items,
    // but to map them to a single model, a filter is needed.
    let customQuery = "items?system.type=article"

    // More about strongly-typed models https://github.com/Kentico/cloud-sdk-swift#using-strongly-typed-models
    client.getItems(modelType: Article.self, customQuery: customQuery) { (isSuccess, itemsResponse, error) in
        if isSuccess {

    // We get here and itemsResponse != nil but items == nil

            if let articles = itemsResponse?.items {
                for article in articles {


                }
            }
        } else {
            if let error = error {
                print(error)
            }
        }
    }
}

ObjectMapper我相信在触发将 JSON 转换为 Article 对象之前会出现此错误消息。不过我可能是错的。

有人有想法么?

更新有趣的是,如果我请求这样的单个文章对象......

client.getItem(modelType: Article.self, itemName: <codename>) { (isSuccess, itemResponse, error) in
            if isSuccess {

                if let article = itemResponse?.item {
                    // Use your item here
                }
            } else {
                if let error = error {
                    print(error)
                }
            }
        }

...然后它的工作原理。我得到了文章对象。它只是要求所有失败的文章。

4

2 回答 2

3

我将在今天晚些时候调查这个问题,但是,根据您的描述,它可能是由 Delivery API 项目准备延迟引起的 - 该项目尚未与交付 API 完全同步。在发布/取消发布项目或创建/生成项目之后,Delivery API 处理消息时可能会有一点延迟,这可能会导致项目不可用。这种延迟可能是可变的 - 根据我的经验,它可能会从几秒钟到 2-3 分钟不等。尽管如此,我还是要检查一下以确定。我会及时通知你的。

编辑:我很确定在您请求项目时,项目没有在 Delivery API 上同步和处理。API 返回200,这导致回调中的isSuccess为true,但是,可能没有可用项目或只有一部分可用项目 - 我已经重现了这种行为(下面的屏幕截图),尽管它是设计使然(内容/消息在事件中心必须异步处理)。

我还建议改进 Kentico Cloud 的文档,以提及/解释处理来自 Event Hubs 的事件队列消息可能导致的延迟。

调试项目属性

getArticles只是为了确定 - 你可以用你的自定义查询再试一次吗?

Edit2:回到你关于ObjectMapper. 这不是一个错误,只是一条调试消息,但是,调试消息中可能不应该nil只有0(零)。此消息来自:

    private func sendGetItemsRequest<T>(url: String, completionHandler: @escaping (Bool, ItemsResponse<T>?, Error?) -> ()) where T: Mappable {
    sessionManager.request(url, headers: self.headers).responseObject { (response: DataResponse<ItemsResponse<T>>) in

        switch response.result {
        case .success:
            if let value = response.result.value {
                let deliveryItems = value
                if self.isDebugLoggingEnabled {
                    print("[Kentico Cloud] Getting items action has succeeded. Received \(String(describing: deliveryItems.items?.count)) items.")
                }
                completionHandler(true, deliveryItems, nil)
            }
        case .failure(let error):
            if self.isDebugLoggingEnabled {
                print("[Kentico Cloud] Getting items action has failed. Check requested URL: \(url)")
            }
            completionHandler(false, nil, error)
        }
    }
}
于 2019-07-01T07:23:07.593 回答
1

好的。这很奇怪。在通过请求单个项目检查 API 之后(请参阅上面帖子中的更新)并获得结果(woot)。现在看来原始代码(未更改)现在可以工作了。

我想知道数据是否需要一段时间才能传播并在 API 中可用?

谁知道。诡异的。

于 2019-06-28T10:34:30.793 回答