Search code examples
swiftkenticokentico-kontent

Kentico Cloud Swift SDK not returning items


I'm testing out the Kentico Cloud Swift SDK to return some 'article' content types (I have created two of them and they are published).

I am using the Boilerplate code as described here:

The result I get is : [Kentico Cloud] Getting items action has succeeded. Received nil items.

My code:

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)
            }
        }
    }
}

I believe this error message would appear before ObjectMapper is triggered to convert the JSON into Article objects. I could be wrong though.

Anyone have any ideas?

UPDATE Interestingly, if I request a single article object like so ...

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)
                }
            }
        }

... then it works. I get the Article object. It's just asking for all of the articles that fails.


Solution

  • I'm going to investigate the issue later today, however, from your description, it might be caused by the Delivery API item readiness delay - the project was not fully synced with the delivery API yet. After the publishing/unpublishing item or creating/generating the project, there might be a small delay in processing messages by Delivery API which could cause unavailability of item. This delay might be variable - from my experience, it may vary from a couple of seconds to 2-3 minutes. Nevertheless, I'm going to check it just to be sure. I'll keep you updated.

    Edit: I'm pretty sure the project was not synced and processed on the Delivery API at the time you were requested the items. The API returned 200, which caused isSuccess in the callback to be true, however, there might have been none or just a subset of items available - I've reproduced this behavior (screenshot below), although it's by design (the content/messages in Event Hub must be processed asynchronously).

    I've also suggested the improvement for Kentico Cloud's documentation to mention/explain the possible delay caused by processing events queue messages from Event Hubs.

    debug items property

    Just to be sure - could you try it again with your getArticles custom query?

    Edit2: Back to your question about the ObjectMapper. This is not an error just a debug message, however, there shouldn't be probably nil but 0 (zero) in the debug message. This message came from:

        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)
            }
        }
    }