Prefetch Lenses
Overview
To present a Lens experience as soon as the user opens up the camera screen, you may want to download the Lens beforehand. This way users don't have to wait for the Lens to be downloaded when they open the camera screen. This also helps when there is poor or no internet connectivity. This is the solution we recommend for those looking to support Lenses in offline mode, instead of bundling Lenses with your application. Lenses can still be experienced from the device's cache memory. It's recommended to prefetch Lenses as soon as the user lands on a page from where they can launch the camera screen. Below are the steps to prefetch Lenses.
Please make sure app has enough cache memory to cache all the Lenses you are prefetching and planning on using when device is offline. Typically you should have 4-8MB/Lens for every cached Lens to avoid cache evictions. App can configure cache using SCCameraKitCacheConfig on iOS.
iOS Implementation
To listen for changes to Lenses that have been updated, conform the class that manages your camera and Lens stack to LensRepositoryGroupObserver. An example implementation is provided in the sample app CameraController.repository(_:didUpdateLenses:forGroupID:)
open func repository(_ repository: LensRepository, didUpdateLenses lenses: [Lens], forGroupID groupID: String) {
// prefetch lens content
if !groupID.contains(SCCameraKitLensRepositoryBundledGroup) {
// the object returned here can be used to cancel the ongoing prefetch operation if need be
_ = cameraKit.lenses.prefetcher.prefetch(lenses: lenses, completion: nil)
for lens in lenses {
cameraKit.lenses.prefetcher.addStatusObserver(self, lens: lens)
}
}
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
let lenses = self.groupIDs.flatMap {
self.cameraKit.lenses.repository.lenses(groupID: $0)
}
// notifies the delegate that new lenses have been prefetched
self.uiDelegate?.cameraController(self, updatedLenses: lenses)
}
}
You may also want the UI to reflect the fact that a Lens is being prefetched, for example, by showing an activity indicator. This can be done by conforming the camera controller to SCCameraKitLensPrefetcherObserver
. See CameraController.prefetcher(_:didUpdate:status:)
for an example implementation.