chrisben / imgcache.js

JS library based on the File API to cache images for offline recovery (target: cordova/phonegap & chrome)
Other
826 stars 216 forks source link

App crashing with cached image on v2.1.0 but not 2.0.0 #244

Open CodeWithOz opened 4 years ago

CodeWithOz commented 4 years ago

We are seeing crashes in our app (Android and iOS) when we move from version 2.0.0 of this library to version 2.1.0. Our project is a Cordova app which uses the Google Maps plugin. We use custom PNGs that have been cached by imgcache, and then supply the nativeURL (gotten using the file plugin) of the cached file to the .addMarker method of the google maps plugin. We do something like this:

        const originalUrl = 'https://ourdomain.com/path/to/our/image.png';
        const loadImg = new Image();
        loadImg.onload = function () {
            ImgCache.isCached(url, function (path, success) {
                if (!success) {
                    console.log('Trying to Cache profile thumbnail for Map Marker...', url);
                    ImgCache.cacheFile(url, () => {
                        ImgCache.getCachedFile(url, function (path, localPath) {
                            console.log('After successful cache, retrieving cached file for Map Marker...', localPath.nativeURL);
                            map.addMarker({
                                // ... other options
                                icon: {
                                    url: localPath.nativeURL,
                                    size: ...
                                }
                            });
                        });
                    });
                } else {
                    ImgCache.getCachedFile(url, function (path, localPath) {
                        console.log('Retrieving cached file for Map Marker...', localPath.nativeURL);
                        map.addMarker({
                            // ... other options
                            icon: {
                                url: localPath.nativeURL,
                                size: ...
                            }
                        });
                    });
                }
            });
        };
        loadImg.onerror = function () {
            // we use a default img that isn't cached
        };
        loadImg.src = originalUrl;

This is an issue describing the errors we see in the android logcat. Curiously on Android, when we switch from a build containing imgcache 2.0.0 to another containing 2.1.0, the crashes don't happen until a new version of the image is cached; after that the app crashes every time we call map.addMarker with the cached file. And then if we go back to a build containing 2.0.0, the crash continues to happen until we cache a new image. I'd greatly appreciate any insights. Project is private but let me know if there's anything else I may be able to provide. Thanks!

Cordova Android: 8.1.0 Cordova iOS: 5.0.1