ios - 在 iOS Carplay 中延迟加载数据
问题描述
用户在 Carplay 中滚动时如何延迟加载项目?
我正在使用 MPPlayableContentDataSource 中的 beginLoadingChildItems 来加载第一组项目,但是当用户滚动到页面底部时如何调用下一页?
解决方案
您可以在以下函数中实现这一点:
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
}
推荐阅读
- javascript - 自定义 - 用于验证自定义电子邮件地址的 Javascript 正则表达式
- android - BottomNavigationView 布局问题
- ios - 快速从数组中获取开始日期和结束日期之间的日期
- node.js - 如何修复节点模块 live-server 以使用 require('open') 而不是 require('opn')
- javascript - 如何使用扩展运算符取消现有数组的新值?
- php - 当表名是变量时,mysqli_fetch_all() 不起作用
- ruby-on-rails - 如何修复“未定义的方法‘空’?对于...:浮动“
- c++ - 重构现有的包装器类以使用包装器本身的相同构造函数参数包装 T
- php - 使用 PHP 从锚标签中获取 id
- javascript - 为什么 mat-error 在 MatDialog 上工作,但在普通页面上却不行?