gpt4 book ai didi

ios - iOS Carplay 中的延迟加载数据

转载 作者:行者123 更新时间:2023-12-01 16:45:12 24 4
gpt4 key购买 nike

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

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

enter image description here

最佳答案

实现此目的的方法是在以下函数中:

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

例如:

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

// Load the next corresponding batch

}

加载您的惰性数据,然后调用:

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

,但首先,您需要在以下函数中正确返回总项目数:

func numberOfChildItems(at indexPath: IndexPath) -> Int
<小时/>

类似下面的内容,

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

}

关于ios - iOS Carplay 中的延迟加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59501929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com