Skip to content

Commit bb7f481

Browse files
author
Mallory Paine
committed
FICImageCache now can be instantiated as a normal object. You're no longer required to use the singleton sharedImageCache.
1 parent 3db6336 commit bb7f481

File tree

5 files changed

+25
-15
lines changed

5 files changed

+25
-15
lines changed

FastImageCache/FICImageCache.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ - (void)setFormats:(NSArray *)formats {
100100
FICImageFormatDevices devices = [imageFormat devices];
101101
if (devices & currentDevice) {
102102
// Only initialize an image table for this format if it is needed on the current device.
103-
FICImageTable *imageTable = [[FICImageTable alloc] initWithFormat:imageFormat];
103+
FICImageTable *imageTable = [[FICImageTable alloc] initWithFormat:imageFormat imageCache:self];
104104
[_imageTables setObject:imageTable forKey:formatName];
105105
[_formats setObject:imageFormat forKey:formatName];
106106

FastImageCache/FICImageTable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ extern NSString *const FICImageTableScreenScaleKey;
7979
8080
@warning `FICImageTable` raises an exception if `imageFormat` is `nil`. `FICImageTable`'s implementation of `-init` simply calls through to this initializer, passing `nil` for `imageFormat`.
8181
*/
82-
- (instancetype)initWithFormat:(FICImageFormat *)imageFormat;
82+
- (instancetype)initWithFormat:(FICImageFormat *)imageFormat imageCache:(FICImageCache *)imageCache;
8383

8484
///------------------------------------------------
8585
/// @name Storing, Retrieving, and Deleting Entries

FastImageCache/FICImageTable.m

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ @interface FICImageTable () {
6565

6666
BOOL _isFileDataProtected;
6767
}
68-
68+
@property(nonatomic, weak) FICImageCache *imageCache;
6969
@end
7070

7171
#pragma mark
@@ -123,13 +123,18 @@ + (NSString *)directoryPath {
123123

124124
#pragma mark - Object Lifecycle
125125

126-
- (instancetype)initWithFormat:(FICImageFormat *)imageFormat {
126+
- (instancetype)initWithFormat:(FICImageFormat *)imageFormat imageCache:(FICImageCache *)imageCache {
127127
self = [super init];
128128

129129
if (self != nil) {
130130
if (imageFormat == nil) {
131131
[NSException raise:NSInvalidArgumentException format:@"*** FIC Exception: %s must pass in an image format.", __PRETTY_FUNCTION__];
132132
}
133+
if (imageCache == nil) {
134+
[NSException raise:NSInvalidArgumentException format:@"*** FIC Exception: %s must pass in an image cache.", __PRETTY_FUNCTION__];
135+
}
136+
137+
self.imageCache = imageCache;
133138

134139
_lock = [[NSRecursiveLock alloc] init];
135140
_indexNumbers = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, &kCFTypeDictionaryValueCallBacks);
@@ -185,7 +190,7 @@ - (instancetype)initWithFormat:(FICImageFormat *)imageFormat {
185190
_entriesPerChunk = MAX(4, goalEntriesPerChunk);
186191
if ([self _maximumCount] > [_imageFormat maximumCount]) {
187192
NSString *message = [NSString stringWithFormat:@"*** FIC Warning: growing desired maximumCount (%ld) for format %@ to fill a chunk (%d)", (long)[_imageFormat maximumCount], [_imageFormat name], [self _maximumCount]];
188-
[[FICImageCache sharedImageCache] _logMessage:message];
193+
[self.imageCache _logMessage:message];
189194
}
190195
_chunkLength = (size_t)(_entryLength * _entriesPerChunk);
191196

@@ -201,7 +206,7 @@ - (instancetype)initWithFormat:(FICImageFormat *)imageFormat {
201206
} else {
202207
// If something goes wrong and we can't open the image table file, then we have no choice but to release and nil self.
203208
NSString *message = [NSString stringWithFormat:@"*** FIC Error: %s could not open the image table file at path %@. The image table was not created.", __PRETTY_FUNCTION__, _filePath];
204-
[[FICImageCache sharedImageCache] _logMessage:message];
209+
[self.imageCache _logMessage:message];
205210

206211
self = nil;
207212
}
@@ -211,7 +216,7 @@ - (instancetype)initWithFormat:(FICImageFormat *)imageFormat {
211216
}
212217

213218
- (instancetype)init {
214-
return [self initWithFormat:nil];
219+
return [self initWithFormat:nil imageCache:nil];
215220
}
216221

217222
- (void)dealloc {
@@ -255,7 +260,7 @@ - (FICImageTableChunk *)_chunkAtIndex:(NSInteger)index {
255260

256261
if (!chunk) {
257262
NSString *message = [NSString stringWithFormat:@"*** FIC Error: %s failed to get chunk for index %ld.", __PRETTY_FUNCTION__, (long)index];
258-
[[FICImageCache sharedImageCache] _logMessage:message];
263+
[self.imageCache _logMessage:message];
259264
}
260265

261266
return chunk;
@@ -377,7 +382,7 @@ - (UIImage *)newImageForEntityUUID:(NSString *)entityUUID sourceImageUUID:(NSStr
377382
CGImageRelease(imageRef);
378383
} else {
379384
NSString *message = [NSString stringWithFormat:@"*** FIC Error: %s could not create a new CGImageRef for entity UUID %@.", __PRETTY_FUNCTION__, entityUUID];
380-
[[FICImageCache sharedImageCache] _logMessage:message];
385+
[self.imageCache _logMessage:message];
381386
}
382387

383388
if (image != nil && preheatData) {
@@ -467,7 +472,7 @@ - (void)_setEntryCount:(NSInteger)entryCount {
467472

468473
if (result != 0) {
469474
NSString *message = [NSString stringWithFormat:@"*** FIC Error: %s ftruncate returned %d, error = %d, fd = %d, filePath = %@, length = %lld", __PRETTY_FUNCTION__, result, errno, _fileDescriptor, _filePath, fileLength];
470-
[[FICImageCache sharedImageCache] _logMessage:message];
475+
[self.imageCache _logMessage:message];
471476
} else {
472477
_fileLength = fileLength;
473478
_entryCount = entryCount;
@@ -516,6 +521,7 @@ - (FICImageTableEntry *)_entryDataAtIndex:(NSInteger)index {
516521
entryData = [[FICImageTableEntry alloc] initWithImageTableChunk:chunk bytes:mappedEntryAddress length:_entryLength];
517522

518523
if (entryData) {
524+
[entryData setImageCache:self.imageCache];
519525
[entryData setIndex:index];
520526
[_chunkSet addObject:chunk];
521527

@@ -536,7 +542,7 @@ - (FICImageTableEntry *)_entryDataAtIndex:(NSInteger)index {
536542
} else {
537543
message = [NSString stringWithFormat:@"*** FIC Error: %s. Cannot get entry data because imageTable's file has data protection enabled and that data is not currently accessible.", __PRETTY_FUNCTION__];
538544
}
539-
[[FICImageCache sharedImageCache] _logMessage:message];
545+
[self.imageCache _logMessage:message];
540546
}
541547

542548
return entryData;
@@ -572,7 +578,7 @@ - (NSInteger)_nextEntryIndex {
572578

573579
if (index >= [self _maximumCount]) {
574580
NSString *message = [NSString stringWithFormat:@"FICImageTable - unable to evict entry from table '%@' to make room. New index %ld, desired max %d", [_imageFormat name], (long)index, [self _maximumCount]];
575-
[[FICImageCache sharedImageCache] _logMessage:message];
581+
[self.imageCache _logMessage:message];
576582
}
577583

578584
return index;
@@ -662,7 +668,7 @@ - (void)saveMetadata {
662668
BOOL fileWriteResult = [data writeToFile:[self metadataFilePath] atomically:NO];
663669
if (fileWriteResult == NO) {
664670
NSString *message = [NSString stringWithFormat:@"*** FIC Error: %s couldn't write metadata for format %@", __PRETTY_FUNCTION__, [_imageFormat name]];
665-
[[FICImageCache sharedImageCache] _logMessage:message];
671+
[self.imageCache _logMessage:message];
666672
}
667673
});
668674
}
@@ -681,7 +687,7 @@ - (void)_loadMetadata {
681687
metadataDictionary = nil;
682688

683689
NSString *message = [NSString stringWithFormat:@"*** FIC Notice: Image format %@ has changed; deleting data and starting over.", [_imageFormat name]];
684-
[[FICImageCache sharedImageCache] _logMessage:message];
690+
[self.imageCache _logMessage:message];
685691
}
686692

687693
[_indexMap setDictionary:[metadataDictionary objectForKey:FICImageTableIndexMapKey]];

FastImageCache/FICImageTableEntry.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "FICImports.h"
1010

1111
@class FICImageTableChunk;
12+
@class FICImageCache;
1213

1314
typedef struct {
1415
CFUUIDBytes _entityUUIDBytes;
@@ -54,6 +55,8 @@ typedef struct {
5455

5556
@property (nonatomic, readonly) FICImageTableChunk *imageTableChunk;
5657

58+
@property (nonatomic, weak) FICImageCache *imageCache;
59+
5760
@property (nonatomic, assign) NSInteger index;
5861

5962
- (void)preheat;

FastImageCache/FICImageTableEntry.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ @implementation FICImageTableEntry
3535
@synthesize length = _length;
3636
@synthesize imageTableChunk = _imageTableChunk;
3737
@synthesize index = _index;
38+
@synthesize imageCache;
3839

3940
#pragma mark - Property Accessors
4041

@@ -113,7 +114,7 @@ - (void)flush {
113114

114115
if (result) {
115116
NSString *message = [NSString stringWithFormat:@"*** FIC Error: %s msync(%p, %ld) returned %d errno=%d", __PRETTY_FUNCTION__, pageAlignedAddress, bytesToFlush, result, errno];
116-
[[FICImageCache sharedImageCache] _logMessage:message];
117+
[self.imageCache _logMessage:message];
117118
}
118119
}
119120

0 commit comments

Comments
 (0)