#1797 (cold boot should preserve feeds and icons)

- Now uses the Documents directory instead of the Caches one, to avoid losing data in low disk space.
- Exisitng data is copied to the new location if needed.
This commit is contained in:
David Sinclair 2023-07-04 15:23:01 -07:00
parent 26734f98a5
commit e6548c7cf7
11 changed files with 35 additions and 66 deletions

View file

@ -2856,9 +2856,7 @@ heightForHeaderInSection:(NSInteger)section {
[userInfoView addSubview:userAvatarButton];
#warning tracing issue #1797
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *rootPath = appDelegate.documentsURL.path;
NSString *PINDiskCachePrefix = @"com.pinterest.PINDiskCache";
NSString *cacheName = @"NBFavicons";
NSString *pathComponent = [[NSString alloc] initWithFormat:@"%@.%@", PINDiskCachePrefix, cacheName];
@ -2868,11 +2866,9 @@ heightForHeaderInSection:(NSInteger)section {
includingPropertiesForKeys:nil
options:NSDirectoryEnumerationSkipsHiddenFiles
error:&error];
NSLog(@"🌄 %@ disk cache contains %@ files; error: %@", cacheName, @(files.count), error); // log
userLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, yOffset, userInfoView.frame.size.width, 16)];
userLabel.text = [NSString stringWithFormat:@"%@ — 🌄 %@ icons", appDelegate.activeUsername, @(files.count)]; // appDelegate.activeUsername;
userLabel.text = appDelegate.activeUsername;
userLabel.font = userLabelFont;
userLabel.textColor = UIColorFromRGB(0x404040);
userLabel.backgroundColor = [UIColor clearColor];

View file

@ -156,6 +156,7 @@ SFSafariViewControllerDelegate> {
PINCache *cachedStoryImages;
}
@property (nonatomic, readonly) NSURL *documentsURL;
@property (nonatomic) SplitViewController *splitViewController;
@property (nonatomic) IBOutlet UINavigationController *ftuxNavigationController;
@property (nonatomic) IBOutlet UINavigationController *feedsNavigationController;

View file

@ -1446,9 +1446,6 @@
[self endNetworkOperation:networkOperationIdentifier];
}];
//#warning tracing issue #1797
// NSLog(@"🌄 start network operation: %@", networkOperationIdentifier); // log
if (backgroundTaskIdentifier != UIBackgroundTaskInvalid) {
self.networkBackgroundTasks[networkOperationIdentifier] = @(backgroundTaskIdentifier);
}
@ -1460,9 +1457,6 @@
UIBackgroundTaskIdentifier identifier = self.networkBackgroundTasks[networkOperationIdentifier].integerValue;
if (identifier != UIBackgroundTaskInvalid) {
//#warning tracing issue #1797 (commenting out -endBackgroundTask: to trigger eviction)
// NSLog(@"🌄 end network operation: %@", networkOperationIdentifier); // log
[[UIApplication sharedApplication] endBackgroundTask:identifier];
}
@ -4003,11 +3997,9 @@
#pragma mark Storing Stories for Offline
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
- (NSURL *)documentsURL
{
NSLog(@" ---> DB dir: %@",[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
}
- (NSInteger)databaseSchemaVersion:(FMDatabase *)db {
@ -4020,35 +4012,28 @@
return version;
}
- (void)copyFrom:(NSURL *)sourceFolderURL to:(NSURL *)destFolderURL name:(NSString *)filename isDirectory:(BOOL)isDirectory {
NSURL *sourceURL = [sourceFolderURL URLByAppendingPathComponent:filename isDirectory:isDirectory];
NSURL *destURL = [destFolderURL URLByAppendingPathComponent:filename isDirectory:isDirectory];
[[NSFileManager defaultManager] copyItemAtURL:sourceURL toURL:destURL error:nil];
}
- (void)createDatabaseConnection {
NSError *error;
// Remove the deletion of old sqlite dbs past version 3.1, once everybody's
// upgraded and removed the old files.
NSURL *documentsURL = self.documentsURL;
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *oldDBPath = [documentPaths objectAtIndex:0];
NSArray *directoryContents = [fileManager contentsOfDirectoryAtPath:oldDBPath error:&error];
int removed = 0;
if (error == nil) {
for (NSString *path in directoryContents) {
NSString *fullPath = [oldDBPath stringByAppendingPathComponent:path];
if ([fullPath hasSuffix:@".sqlite"]) {
[fileManager removeItemAtPath:fullPath error:&error];
removed++;
}
}
}
if (removed) {
NSLog(@"Deleted %d sql dbs.", removed);
}
NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *dbPath = [cachePaths objectAtIndex:0];
NSString *dbPath = documentsURL.path;
NSString *dbName = [NSString stringWithFormat:@"%@.sqlite", self.host];
NSString *path = [dbPath stringByAppendingPathComponent:dbName];
[self applicationDocumentsDirectory];
// Move data from Caches directory to Documents directory.
if (![fileManager fileExistsAtPath:path]) {
NSURL *oldURL = [[fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] firstObject];
[self copyFrom:oldURL to:documentsURL name:@"com.pinterest.PINDiskCache.NBFavicons" isDirectory:YES];
[self copyFrom:oldURL to:documentsURL name:@"com.pinterest.PINDiskCache.NBStoryImages" isDirectory:YES];
[self copyFrom:oldURL to:documentsURL name:@"story_images" isDirectory:YES];
[self copyFrom:oldURL to:documentsURL name:dbName isDirectory:YES];
}
database = [FMDatabaseQueue databaseQueueWithPath:path];
[database inDatabase:^(FMDatabase *db) {
@ -4076,8 +4061,7 @@
// [db executeUpdate:@"drop table if exists `queued_saved_hashes`"]; // Nope, don't clear this.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"story_images"];
NSString *cacheDirectory = [self.documentsURL.path stringByAppendingPathComponent:@"story_images"];
NSError *error = nil;
BOOL success = [fileManager removeItemAtPath:cacheDirectory error:&error];
if (!success || error) {
@ -4215,8 +4199,7 @@
[db executeUpdate:indexUsersUserId];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *storyImagesDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"story_images"];
NSString *storyImagesDirectory = [self.documentsURL.path stringByAppendingPathComponent:@"story_images"];
if (![[NSFileManager defaultManager] fileExistsAtPath:storyImagesDirectory]) {
[[NSFileManager defaultManager] createDirectoryAtPath:storyImagesDirectory
withIntermediateDirectories:NO
@ -4707,8 +4690,7 @@
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"story_images"];
NSString *cacheDirectory = [self.documentsURL.path stringByAppendingPathComponent:@"story_images"];
NSArray *directoryContents = [fileManager contentsOfDirectoryAtPath:cacheDirectory error:&error];
int removed = 0;

View file

@ -391,7 +391,7 @@
NSString* urlString = activeUrl;
NSURL* url = [NSURL URLWithString:urlString];
// if ([urlString containsString:@"story_images"]) {
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *storyImagesDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"story_images"];
//
// urlString = [urlString substringFromIndex:NSMaxRange([urlString

View file

@ -508,8 +508,7 @@
NSString *storyHash = [self.activeStory objectForKey:@"story_hash"];
NSArray *imageUrls = [appDelegate.activeCachedImages objectForKey:storyHash];
if (imageUrls) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *storyImagesDirectory = [[paths objectAtIndex:0]
NSString *storyImagesDirectory = [appDelegate.documentsURL.path
stringByAppendingPathComponent:@"story_images"];
for (NSString *imageUrl in imageUrls) {
NSURL *cachedUrl = [NSURL fileURLWithPath:storyImagesDirectory];

View file

@ -22,7 +22,7 @@
int deleted = 0;
int checked = 0;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"story_images"];
NSDirectoryEnumerator* en = [fileManager enumeratorAtPath:cacheDirectory];
NSDate *d = [[NSDate date] dateByAddingTimeInterval:-30*24*60*60];

View file

@ -171,7 +171,7 @@
// NSLog(@"Storing image: %@ (%d bytes - %d in queue)", storyHash, [responseData length], [imageDownloadOperationQueue requestsCount]);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"story_images"];
NSString *fullPath = [cacheDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", md5Url, [imageUrl pathExtension]]];

View file

@ -12,7 +12,7 @@
@implementation NBURLCache
- (NSString *)substitutePath:(NSString *)pathString {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *storyImagesDirectory = [[paths objectAtIndex:0]
stringByAppendingPathComponent:@"story_images"];
NSString *cachedImage = [[storyImagesDirectory

View file

@ -35,7 +35,7 @@ static NSString * const PINCacheSharedName = @"PINCacheShared";
- (instancetype)initWithName:(NSString *)name
{
return [self initWithName:name rootPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]];
return [self initWithName:name rootPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]];
}
- (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath

View file

@ -79,7 +79,7 @@ typedef NS_ENUM(NSUInteger, PINDiskCacheCondition) {
- (instancetype)initWithName:(NSString *)name
{
return [self initWithName:name rootPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
return [self initWithName:name rootPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
}
- (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath
@ -303,9 +303,6 @@ typedef NS_ENUM(NSUInteger, PINDiskCacheCondition) {
includingPropertiesForKeys:keys
options:NSDirectoryEnumerationSkipsHiddenFiles
error:&error];
#warning tracing issue #1797
NSLog(@"🌄 %@ disk cache contains %@ files; error: %@", _name, @(files.count), error); // log
PINDiskCacheError(error);
for (NSURL *fileURL in files) {
@ -664,12 +661,6 @@ typedef NS_ENUM(NSUInteger, PINDiskCacheCondition) {
[self lock];
fileURL = [self encodedFileURLForKey:key];
object = nil;
// #warning hack; this will simulate issue #1797
// fileURL = nil;
#warning tracing issue #1797
NSLog(@"🌄 %@ key: %@, URL: %@ %@", _name, key, fileURL, [[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]] ? @"✅" : @"🚫"); // log
if ([[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]] &&
// If the cache should behave like a TTL cache, then only fetch the object if there's a valid ageLimit and the object is still alive

View file

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="21507" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="6pv-7g-17r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="21701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="6pv-7g-17r">
<device id="ipad11_0rounded" orientation="landscape" layout="fullscreen" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21505"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21679"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>