【问题标题】:Kentico Cloud Swift SDK not returning itemsKentico Cloud Swift SDK 不返回项目
【发布时间】:2019-06-28 09:02:15
【问题描述】:

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

我正在使用 here: 中描述的样板代码

我得到的结果是:[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)
                }
            }
        }

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

【问题讨论】:

    标签: swift kentico kentico-kontent


    【解决方案1】:

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

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

    我还建议改进 Kentico Cloud 的文档,以提及/解释 processing events queue messages from 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)
            }
        }
    }
    

    【讨论】:

    • 我的代码现在可以正常工作:[Kentico Cloud] 获取项目操作已成功。收到可选 (2) 项。
    • 虽然处理项目或项目的延迟会很大,但我尝试了很长时间的调用,但它只是在我请求单个项目后才开始工作。
    • 我几乎可以肯定这只是一个巧合 - 我不熟悉任何可能以获取所有物品而不是只获取一件物品为条件的内部流程。但是,如果您将来遇到任何可疑行为,请告诉我。最后 - 它是软件,可能存在一些故障。
    【解决方案2】:

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

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

    谁知道呢。很奇怪。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-24
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      • 1970-01-01
      • 2015-03-14
      相关资源
      最近更新 更多