Moving offline image clenaing to nsoperation to speed up refresh time.

This commit is contained in:
Samuel Clay 2013-08-05 18:32:43 -07:00
parent e9e2d9f729
commit 801c1346ae
11 changed files with 121 additions and 39 deletions

View file

@ -25,6 +25,8 @@ static UIFont *indicatorFont = nil;
@synthesize siteTitle;
@synthesize siteFavicon;
@synthesize isRead;
@synthesize isStarred;
@synthesize isShared;
@synthesize isShort;
@synthesize isRiverOrSocial;
@synthesize feedColorBar;

View file

@ -899,7 +899,7 @@
unsigned int colorBorder = 0;
NSString *faviconColor = [feed valueForKey:@"favicon_fade"];
if ([faviconColor class] == [NSNull class]) {
if ([faviconColor class] == [NSNull class] || !faviconColor) {
faviconColor = @"707070";
}
NSScanner *scannerBorder = [NSScanner scannerWithString:faviconColor];
@ -909,7 +909,7 @@
// feed color bar border
NSString *faviconFade = [feed valueForKey:@"favicon_color"];
if ([faviconFade class] == [NSNull class]) {
if ([faviconFade class] == [NSNull class] || !faviconFade) {
faviconFade = @"505050";
}
scannerBorder = [NSScanner scannerWithString:faviconFade];

View file

@ -142,7 +142,7 @@
}
- (void)setProgress:(float)value {
[self.progressBar setProgress:value animated:(self.style == NBSyncingProgressStyle)];
[self.progressBar setProgress:value animated:NO];
}
- (void)setTitle:(NSString *)title {

View file

@ -141,6 +141,7 @@
FMDatabaseQueue *database;
NSOperationQueue *offlineQueue;
NSOperationQueue *offlineCleaningQueue;
NSArray *categories;
NSDictionary *categoryFeeds;
UIImageView *splashView;
@ -243,6 +244,7 @@
@property (nonatomic) NSDictionary *categoryFeeds;
@property (readwrite) FMDatabaseQueue *database;
@property (nonatomic) NSOperationQueue *offlineQueue;
@property (nonatomic) NSOperationQueue *offlineCleaningQueue;
@property (nonatomic) NSMutableDictionary *activeCachedImages;
@property (nonatomic, readwrite) BOOL hasQueuedReadStories;
@ -353,7 +355,6 @@
- (void)flushQueuedReadStories:(BOOL)forceCheck withCallback:(void(^)())callback;
- (void)syncQueuedReadStories:(FMDatabase *)db withStories:(NSDictionary *)hashes withCallback:(void(^)())callback;
- (void)prepareActiveCachedImages:(FMDatabase *)db;
- (void)flushOldCachedImages;
- (void)deleteAllCachedImages;
@end

View file

@ -46,6 +46,7 @@
#import "OfflineSyncUnreads.h"
#import "OfflineFetchStories.h"
#import "OfflineFetchImages.h"
#import "OfflineCleanImages.h"
#import "PocketAPI.h"
@implementation NewsBlurAppDelegate
@ -154,6 +155,7 @@
@synthesize activeCachedImages;
@synthesize hasQueuedReadStories;
@synthesize offlineQueue;
@synthesize offlineCleaningQueue;
+ (NewsBlurAppDelegate*) sharedAppDelegate {
return (NewsBlurAppDelegate*) [UIApplication sharedApplication].delegate;
@ -2275,6 +2277,9 @@
if (offlineQueue) {
[offlineQueue cancelAllOperations];
}
if (offlineCleaningQueue) {
[offlineCleaningQueue cancelAllOperations];
}
}
- (void)startOfflineQueue {
@ -2307,10 +2312,11 @@
- (void)flushQueuedReadStories:(BOOL)forceCheck withCallback:(void(^)())callback {
if (self.hasQueuedReadStories || forceCheck) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
(unsigned long)NULL), ^(void) {
[self flushOldCachedImages];
});
OfflineCleanImages *operationCleanImages = [[OfflineCleanImages alloc] init];
if (!offlineCleaningQueue) {
offlineCleaningQueue = [NSOperationQueue new];
}
[offlineCleaningQueue addOperation:operationCleanImages];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
(unsigned long)NULL), ^(void) {
@ -2397,36 +2403,6 @@
NSLog(@"Pre-cached %d images", cached);
}
- (void)flushOldCachedImages {
int deleted = 0;
int checked = 0;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"story_images"];
NSDirectoryEnumerator* en = [fileManager enumeratorAtPath:cacheDirectory];
NSDate *d = [[NSDate date] dateByAddingTimeInterval:-14*24*60*60];
NSDateFormatter *df = [[NSDateFormatter alloc] init]; // = [NSDateFormatter initWithDateFormat:@"yyyy-MM-dd"];
[df setDateFormat:@"EEEE d"];
NSString *filepath;
NSDate *creationDate;
NSString* file;
while (file = [en nextObject])
{
filepath = [NSString stringWithFormat:[cacheDirectory stringByAppendingString:@"/%@"],file];
creationDate = [[fileManager attributesOfItemAtPath:filepath error:nil] fileCreationDate];
checked += 1;
if ([creationDate compare:d] == NSOrderedAscending) {
[[NSFileManager defaultManager]
removeItemAtPath:[cacheDirectory stringByAppendingPathComponent:file]
error:nil];
deleted += 1;
}
}
NSLog(@"Deleted %d/%d old cached images", deleted, checked);
}
- (void)deleteAllCachedImages {
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSError *error = nil;

View file

@ -0,0 +1,13 @@
//
// OfflineCleanImages.h
// NewsBlur
//
// Created by Samuel Clay on 8/5/13.
// Copyright (c) 2013 NewsBlur. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface OfflineCleanImages : NSOperation
@end

View file

@ -0,0 +1,13 @@
//
// OfflineCleanImages.m
// NewsBlur
//
// Created by Samuel Clay on 8/5/13.
// Copyright (c) 2013 NewsBlur. All rights reserved.
//
#import "OfflineCleanImages.h"
@implementation OfflineCleanImages
@end

View file

@ -0,0 +1,16 @@
//
// OfflineCleanImages.h
// NewsBlur
//
// Created by Samuel Clay on 8/5/13.
// Copyright (c) 2013 NewsBlur. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "NewsBlurAppDelegate.h"
@interface OfflineCleanImages : NSOperation
@property (nonatomic) NewsBlurAppDelegate *appDelegate;
@end

View file

@ -0,0 +1,55 @@
//
// OfflineCleanImages.m
// NewsBlur
//
// Created by Samuel Clay on 8/5/13.
// Copyright (c) 2013 NewsBlur. All rights reserved.
//
#import "OfflineCleanImages.h"
@implementation OfflineCleanImages
@synthesize appDelegate;
- (void)main {
appDelegate = [NewsBlurAppDelegate sharedAppDelegate];
NSLog(@"Cleaning stale offline images...");
int deleted = 0;
int checked = 0;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"story_images"];
NSDirectoryEnumerator* en = [fileManager enumeratorAtPath:cacheDirectory];
NSDate *d = [[NSDate date] dateByAddingTimeInterval:-14*24*60*60];
NSDateFormatter *df = [[NSDateFormatter alloc] init]; // = [NSDateFormatter initWithDateFormat:@"yyyy-MM-dd"];
[df setDateFormat:@"EEEE d"];
NSString *filepath;
NSDate *creationDate;
NSString* file;
while (file = [en nextObject])
{
filepath = [NSString stringWithFormat:[cacheDirectory stringByAppendingString:@"/%@"],file];
creationDate = [[fileManager attributesOfItemAtPath:filepath error:nil] fileCreationDate];
checked += 1;
if ([creationDate compare:d] == NSOrderedAscending) {
[[NSFileManager defaultManager]
removeItemAtPath:[cacheDirectory stringByAppendingPathComponent:file]
error:nil];
deleted += 1;
}
if (self.isCancelled) {
NSLog(@"Canceling image cleaning...");
break;
}
}
NSLog(@"Deleted %d/%d old cached images", deleted, checked);
}
@end

View file

@ -187,6 +187,7 @@
78FC34FA11CA94900055C312 /* SBJsonBase.m in Sources */ = {isa = PBXBuildFile; fileRef = 78FC34F211CA94900055C312 /* SBJsonBase.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
78FC34FB11CA94900055C312 /* SBJsonParser.m in Sources */ = {isa = PBXBuildFile; fileRef = 78FC34F411CA94900055C312 /* SBJsonParser.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
78FC34FC11CA94900055C312 /* SBJsonWriter.m in Sources */ = {isa = PBXBuildFile; fileRef = 78FC34F611CA94900055C312 /* SBJsonWriter.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
FF0FAEB117B084D2008651F9 /* OfflineCleanImages.m in Sources */ = {isa = PBXBuildFile; fileRef = FF0FAEB017B0846C008651F9 /* OfflineCleanImages.m */; };
FF11045F176950F900502C29 /* NBLoadingCell.m in Sources */ = {isa = PBXBuildFile; fileRef = FF11045E176950F900502C29 /* NBLoadingCell.m */; };
FF1104611769695A00502C29 /* g_icn_offline@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = FF1104601769695A00502C29 /* g_icn_offline@2x.png */; };
FF1660C816D6E9A700AF8541 /* DashboardViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = FF1660C716D6E9A700AF8541 /* DashboardViewController.xib */; };
@ -711,6 +712,8 @@
78FC34F511CA94900055C312 /* SBJsonWriter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SBJsonWriter.h; sourceTree = "<group>"; };
78FC34F611CA94900055C312 /* SBJsonWriter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SBJsonWriter.m; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* NewsBlur-iPhone-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "NewsBlur-iPhone-Info.plist"; plistStructureDefinitionIdentifier = "com.apple.xcode.plist.structure-definition.iphone.info-plist"; sourceTree = "<group>"; };
FF0FAEAF17B0846C008651F9 /* OfflineCleanImages.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = OfflineCleanImages.h; path = offline/OfflineCleanImages.h; sourceTree = "<group>"; };
FF0FAEB017B0846C008651F9 /* OfflineCleanImages.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = OfflineCleanImages.m; path = offline/OfflineCleanImages.m; sourceTree = "<group>"; };
FF11045D176950F900502C29 /* NBLoadingCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NBLoadingCell.h; sourceTree = "<group>"; };
FF11045E176950F900502C29 /* NBLoadingCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NBLoadingCell.m; sourceTree = "<group>"; };
FF1104601769695A00502C29 /* g_icn_offline@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "g_icn_offline@2x.png"; sourceTree = "<group>"; };
@ -1909,6 +1912,8 @@
FF855B5D1794B0760098D48A /* OfflineFetchStories.m */,
FF855B5F1794B0830098D48A /* OfflineFetchImages.h */,
FF855B601794B0830098D48A /* OfflineFetchImages.m */,
FF0FAEAF17B0846C008651F9 /* OfflineCleanImages.h */,
FF0FAEB017B0846C008651F9 /* OfflineCleanImages.m */,
);
name = Offline;
sourceTree = "<group>";
@ -2470,6 +2475,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
FF0FAEB117B084D2008651F9 /* OfflineCleanImages.m in Sources */,
FFFC608517165A1D00DC22E2 /* THCircularProgressView.m in Sources */,
43F44B1C159D8DBC00F48F8A /* FeedTableCell.m in Sources */,
1D3623260D0F684500981E51 /* NewsBlurAppDelegate.m in Sources */,

View file

@ -20,7 +20,7 @@
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define BACKGROUND_REFRESH_SECONDS -10*60
#define BACKGROUND_REFRESH_SECONDS -10
#define NEWSBLUR_LINK_COLOR 0x405BA8
#define NEWSBLUR_HIGHLIGHT_COLOR 0xd2e6fd