在iOS Carplay中延迟加载数据

问题描述

The way you can achieve this is inside the following function:

func beginLoadingChildItems(at indexPath: IndexPath, completionHandler: @escaping (Error?) -> Void)

As Example:

if (indexPath[componentIndex] + 1) % Threshold == 0 { // Threshold is Your Defined Batch Size

    // Load the next corresponding batch

}

Load your lazy data, then call:

completionHandler(nil) // In case of no error has occurred

,but firstly, you need to return the total items count correctly in the following function:

func numberOfChildItems(at indexPath: IndexPath) -> Int

Something like the below,

class YourDataSource : MPPlayableContentDataSource {

    private var items = [MPContentItem]()
    private var currentBatch = 0

    func beginLoadingChildItems(at indexPath: IndexPath, completionHandler: @escaping (Error?) -> Void) {

        // indexPath[1]: is current list level, as per CarPlay list indexing (It's an array of the indices as ex: indexPath = [0,1] means Index 0 in the first level and index 1 at the second level).
        // % 8: Means each 8 items, I will perform the corresponding action.
       // currentBatch + 1 == nextBatch, This check in order to ensure that you load the batches in their sequences.

        let currentCount = indexPath[1] + 1

        let nextBatch = (currentCount / 8) + 1

        if currentCount % 8 == 0 && currentBatch + 1 == nextBatch {

            // Load the next corresponding batch 
            YourAPIHandler.asyncCall { newItems in

                self.currentBatch = self.currentBatch + 1

                items.append(newItems)

                completionHandler(nil)

                MPPlayableContentManager.shared().reloadData()

            }

        } else {

            completionHandler(nil)

        }


    }

}

func numberOfChildItems(at indexPath: IndexPath) -> Int {

    return self.items.count

}

解决方法

用户在Carplay中滚动时如何延迟加载项目?

我正在使用MPPlayableContentDataSource中的beginLoadingChildItems加载
第一组项目,但是当用户滚动到
页面底部时如何调用下一页?

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...