Lazy Loading Data in 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
}
Apple Carplay - how to create a playable container?
I've been working CarPlay for a while now. I'm pretty sure the documentation is wrong. You can't create a playable container.
Carplay: MPPlayableContentDelegate methods never called
It turns out that MPPlayableContentManager
only retains a weak reference to the content delegate. In doing some cleanup, I had inadvertently removed the strong reference that keep the object from being released. Restoring the strong reference caused the object to be retained and fixed the problem.
Carplay Application not showing on iOS 13 simulator
Xcode 11.0 and 11.2.1 versions not showing carplay apps in simulator. It was a xcode bug. It's working now on Xcode 11.3 beta.
iOS 15 UIWindowScene not working properly
So apparently, I made a mistake when I created the info.plist
When I replaced "UIWindowSceneSessionRoleApplication" to "CPTemplateApplicationSceneSessionRoleApplication" it did load and it looks alright
Related Topics
Getting Device Orientation in Swift
How to Manually Deprecate Members
Realmswift: Convert Results to Swift Array
How to Access File Included in App Bundle in Swift
Sharing Code Between Original iOS App and App Extension
Access Container View Controller from Parent iOS
Implement Document Picker in Swift (Ios)
Module Compiled with Swift 4.0 Cannot Be Imported in Swift 4.0.1
How to Change Uisearchbar Placeholder and Image Tint Color
Gradient Polyline with Mapkit iOS
How to Set Image for Bar Button with Swift
How to Center a Popoverview in Swift
Swift - Which Types to Use? Nsstring or String
How to Get Touches When Parent View Has Userinteractionenabled Set to No in iOS
Exc_Bad_Access When Building Nspredicate
Autolayout - Make Height of View Relative to Half Superview Height