I am using SDwebimage Framework for caching images, so downloading number of images increases daily, so after 3-4 days of using application, causes increasing cache,which causes application performance degrading, is there anyway to improve performance
Asked
Active
Viewed 3,680 times
2 Answers
0
Take a look at this SO post here
You can set the period for how long the cache will last, and then it will be flushed. Also in addition, you can play around with the maxCacheSize value in SDImageCache.m in the cleanDiskWithCompletionBlock.
// If our remaining disk cache exceeds a configured maximum size, perform a second
// size-based cleanup pass. We delete the oldest files first.
if (self.maxCacheSize > 0 && currentCacheSize > self.maxCacheSize) {
// Target half of our maximum cache size for this cleanup pass.
const NSUInteger desiredCacheSize = self.maxCacheSize / 2;
// Sort the remaining cache files by their last modification time (oldest first).
NSArray *sortedFiles = [cacheFiles keysSortedByValueWithOptions:NSSortConcurrent
usingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1[NSURLContentModificationDateKey] compare:obj2[NSURLContentModificationDateKey]];
}];
// Delete files until we fall below our desired cache size.
for (NSURL *fileURL in sortedFiles) {
if ([_fileManager removeItemAtURL:fileURL error:nil]) {
NSDictionary *resourceValues = cacheFiles[fileURL];
NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];
currentCacheSize -= [totalAllocatedSize unsignedIntegerValue];
if (currentCacheSize < desiredCacheSize) {
break;
}
}
}
}

Community
- 1
- 1

AshesToAshes
- 937
- 3
- 14
- 31
-
Thanks a lot @ashes to ashes – user1330379 Jun 29 '15 at 08:43
-
how to set some maximum limit like 20 mb in the above block of code @AshesToAshes – R. Mohan Oct 25 '17 at 10:21
-
@R.Mohan did u find some way to limit max file size? – Ankit Kumar Gupta Feb 21 '22 at 15:38
-
I did not remember that @AnkitKumarGupta – R. Mohan Feb 28 '22 at 14:11
0
You can simply set a global cache value for your shared instance of SDImageCache.
Just call the following on your AppDelegate:
// limit SDImage cache to 50 MGB
SDImageCache.shared().config.maxCacheSize = 1_000_000 * 50

Mert Kahraman
- 445
- 6
- 10
-
I'm not an ios developer, I need to configure my cache for react native in my app delegate. What's the import statement and which callback would I put it in the app delegate? – evanjmg Dec 26 '18 at 23:31