Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Use only the managed object context

  • Loading branch information...
commit b105809be6acb1b78437ff124dbb711ff3767a96 1 parent aabea1e
Daniel Tull authored November 24, 2012

Showing 1 changed file with 17 additions and 25 deletions. Show diff stats Hide diff stats

  1. 42  DCTImageCache/_DCTDiskImageCache.m
42  DCTImageCache/_DCTDiskImageCache.m
@@ -13,8 +13,6 @@
13 13
 @implementation _DCTDiskImageCache {
14 14
 	NSURL *_storeURL;
15 15
 	NSManagedObjectContext *_managedObjectContext;
16  
-	NSManagedObjectContext *_savingContext;
17  
-	NSManagedObjectContext *_fetchingContext;
18 16
 }
19 17
 
20 18
 + (NSBundle *)bundle {
@@ -54,12 +52,6 @@ - (void)_createStack {
54 52
 
55 53
 	_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
56 54
 	_managedObjectContext.persistentStoreCoordinator = coordinator;
57  
-
58  
-	_savingContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
59  
-	_savingContext.parentContext = _managedObjectContext;
60  
-
61  
-	_fetchingContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
62  
-	_fetchingContext.parentContext = _managedObjectContext;
63 55
 }
64 56
 
65 57
 - (void)removeAllImages {
@@ -68,12 +60,12 @@ - (void)removeAllImages {
68 60
 }
69 61
 
70 62
 - (void)removeImageForKey:(NSString *)key size:(CGSize)size {
71  
-	[_savingContext performBlock:^{
  63
+	[_managedObjectContext performBlock:^{
72 64
 		NSFetchRequest *fetchRequest = [self _fetchRequestForKey:key size:size];
73  
-		NSArray *items = [_savingContext executeFetchRequest:fetchRequest error:NULL];
  65
+		NSArray *items = [_managedObjectContext executeFetchRequest:fetchRequest error:NULL];
74 66
 		for (_DCTImageCacheItem *item in items)
75  
-			[_savingContext deleteObject:item];
76  
-		[_savingContext save:NULL];
  67
+			[_managedObjectContext deleteObject:item];
  68
+		[_managedObjectContext save:NULL];
77 69
 		[_managedObjectContext performBlock:^{
78 70
 			[_managedObjectContext save:NULL];
79 71
 		}];
@@ -81,12 +73,12 @@ - (void)removeImageForKey:(NSString *)key size:(CGSize)size {
81 73
 }
82 74
 
83 75
 - (void)removeAllImagesForKey:(NSString *)key {
84  
-	[_savingContext performBlock:^{
  76
+	[_managedObjectContext performBlock:^{
85 77
 		NSFetchRequest *fetchRequest = [self _fetchRequestForKey:key];
86  
-		NSArray *items = [_savingContext executeFetchRequest:fetchRequest error:NULL];
  78
+		NSArray *items = [_managedObjectContext executeFetchRequest:fetchRequest error:NULL];
87 79
 		for (_DCTImageCacheItem *item in items)
88  
-			[_savingContext deleteObject:item];
89  
-		[_savingContext save:NULL];
  80
+			[_managedObjectContext deleteObject:item];
  81
+		[_managedObjectContext save:NULL];
90 82
 		[_managedObjectContext performBlock:^{
91 83
 			[_managedObjectContext save:NULL];
92 84
 		}];
@@ -94,13 +86,13 @@ - (void)removeAllImagesForKey:(NSString *)key {
94 86
 }
95 87
 
96 88
 - (void)setImage:(UIImage *)image forKey:(NSString *)key size:(CGSize)size {
97  
-	[_savingContext performBlock:^{
98  
-		_DCTImageCacheItem *item = [_DCTImageCacheItem insertInManagedObjectContext:_savingContext];
  89
+	[_managedObjectContext performBlock:^{
  90
+		_DCTImageCacheItem *item = [_DCTImageCacheItem insertInManagedObjectContext:_managedObjectContext];
99 91
 		item.key = key;
100 92
 		item.sizeString = NSStringFromCGSize(size);
101 93
 		item.imageData = UIImagePNGRepresentation(image);
102 94
 		item.date = [NSDate new];
103  
-		[_savingContext save:NULL];
  95
+		[_managedObjectContext save:NULL];
104 96
 		[_managedObjectContext performBlock:^{
105 97
 			[_managedObjectContext save:NULL];
106 98
 		}];
@@ -109,9 +101,9 @@ - (void)setImage:(UIImage *)image forKey:(NSString *)key size:(CGSize)size {
109 101
 
110 102
 - (UIImage *)imageForKey:(NSString *)key size:(CGSize)size {
111 103
 	__block UIImage *image;
112  
-	[_fetchingContext performBlockAndWait:^{
  104
+	[_managedObjectContext performBlockAndWait:^{
113 105
 		NSFetchRequest *fetchRequest = [self _fetchRequestForKey:key size:size];
114  
-		NSArray *items = [_fetchingContext executeFetchRequest:fetchRequest error:NULL];
  106
+		NSArray *items = [_managedObjectContext executeFetchRequest:fetchRequest error:NULL];
115 107
 		_DCTImageCacheItem *item = [items lastObject];
116 108
 		image = [UIImage imageWithData:item.imageData];
117 109
 	}];
@@ -120,18 +112,18 @@ - (UIImage *)imageForKey:(NSString *)key size:(CGSize)size {
120 112
 
121 113
 - (BOOL)hasImageForKey:(NSString *)key size:(CGSize)size {
122 114
 	__block BOOL hasImage = NO;
123  
-	[_fetchingContext performBlockAndWait:^{
  115
+	[_managedObjectContext performBlockAndWait:^{
124 116
 		NSFetchRequest *fetchRequest = [self _fetchRequestForKey:key size:size];
125  
-		NSUInteger count = [_fetchingContext countForFetchRequest:fetchRequest error:NULL];
  117
+		NSUInteger count = [_managedObjectContext countForFetchRequest:fetchRequest error:NULL];
126 118
 		hasImage = count > 0;
127 119
 	}];
128 120
 	return hasImage;
129 121
 }
130 122
 
131 123
 - (void)fetchImageForKey:(NSString *)key size:(CGSize)size handler:(void (^)(UIImage *))handler {
132  
-	[_fetchingContext performBlock:^{
  124
+	[_managedObjectContext performBlock:^{
133 125
 		NSFetchRequest *fetchRequest = [self _fetchRequestForKey:key size:size];
134  
-		NSArray *items = [_fetchingContext executeFetchRequest:fetchRequest error:NULL];
  126
+		NSArray *items = [_managedObjectContext executeFetchRequest:fetchRequest error:NULL];
135 127
 		_DCTImageCacheItem *item = [items lastObject];
136 128
 		UIImage *image = [UIImage imageWithData:item.imageData];
137 129
 		handler(image);

0 notes on commit b105809

Please sign in to comment.
Something went wrong with that request. Please try again.