在iOS Carplay中延迟加载数据 [英] Lazy Loading Data in iOS Carplay

查看:235
本文介绍了在iOS Carplay中延迟加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to lazy load the items while the user scrolling in Carplay ?

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

I am using beginLoadingChildItems from MPPlayableContentDataSource to load the first set of items but how I can call the next page when a user scrolls to the bottom of the page?

推荐答案

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

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

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 

}

加载您的惰性数据,然后致电:

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

}

这篇关于在iOS Carplay中延迟加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆