Nuke icon indicating copy to clipboard operation
Nuke copied to clipboard

Setting up pipeline for use in both app and notification service extension

Open JValldejuli opened this issue 9 months ago • 1 comments

Hello, I'm using Nuke to load images in my app. I recently added a notification service extension which serves to load images and attach them to incoming remote notifications. Both the app and the notification service extension are in the same app group. I figured it would be nice if they could share the same disk cache to avoid downloading/processing the same images twice. So in both my app and notification service extension I setup a pipeline that points to the app group directory:

var configuration = ImagePipeline.Configuration()
configuration.dataLoader = {
    let config = URLSessionConfiguration.default
    config.urlCache = nil
    return DataLoader(configuration: config)
}()
configuration.dataCache = {
    guard let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: AppGroupIdentifier) else {
        Log.error("Failed to initialize image pipeline data cache: Could not locate container URL")
        return nil
    }
    let dataCachePath = containerURL.appendingPathComponent(ImageDataCacheName, isDirectory: true)
    return try? DataCache(path: dataCachePath)
}()

My estimation is that this is fine, but I'm wondering if there are any risks associated with doing this. Is there some chance of a crash due to the extension and the app attempting to write to the same spot? I'm guessing no given that I don't show notifications while the app is in the foreground (which is when the app image pipeline would be active). Basically I just wanted to get some feedback regarding whether this approach is reasonable or not. Thank you.

JValldejuli avatar Mar 07 '25 22:03 JValldejuli

For anybody wondering, this has been working well so far!

JValldejuli avatar Apr 18 '25 08:04 JValldejuli