Skip to content

Commit

Permalink
Fixed an issue when operations are frozen/restored when cachin is not…
Browse files Browse the repository at this point in the history
… enabled
  • Loading branch information
MKSG-MugunthKumar committed Nov 29, 2011
1 parent dfe29ff commit dc4aa45
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Documentation/MKNetworkOperation_h/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ <h1><a name="MKNetworkOperation.h">MKNetworkOperation.h</a></h1>
<div class="spec_sheet_info_box"><table cellspacing="0" class="specbox"><tr>
<td scope="row"><b>Includes:</b></td>
<td><div style="margin-bottom:1px"><div class="content_text">
<!-- a logicalPath="//apple_ref/doc/header/UIKit.h" machineGenerated="true" -->&lt;UIKit/UIKit.h&gt;<!-- /a -->
<!-- a logicalPath="//apple_ref/doc/header/UIKit.h" machineGenerated="true" -->&lt;UIKit/UIKit.h&gt;<!-- /a --><br><!-- a logicalPath="//apple_ref/doc/header/MKNetworkKit.h" machineGenerated="true" -->"MKNetworkKit.h"<!-- /a -->
</div></div></td>
</tr></table></div>
<h2>Introduction</h2>
Expand Down
10 changes: 5 additions & 5 deletions MKNetworkKit/MKNetworkEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@
#import <Foundation/Foundation.h>
#import "MKNetworkKit.h"

/*!
@header MKNetworkEngine.h
@abstract Represents a subclassable Network Engine for your app
*/

@class MKNetworkOperation;

typedef void (^ProgressBlock)(double progress);
typedef void (^ResponseBlock)(MKNetworkOperation* operation);
typedef void (^ImageBlock) (UIImage* fetchedImage, NSString* urlString);
typedef void (^ErrorBlock)(NSError* error);

/*!
@header MKNetworkEngine.h
@abstract Represents a subclassable Network Engine for your app
*/

/*!
* @class MKNetworkEngine
* @abstract Represents a subclassable Network Engine for your app
Expand Down
14 changes: 14 additions & 0 deletions MKNetworkKit/MKNetworkEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ -(void) saveCacheData:(NSData*) data forKey:(NSString*) cacheDataKey;
-(void) freezeOperations;
-(void) checkAndRestoreFrozenOperations;

-(BOOL) isCacheEnabled;
@end

static NSOperationQueue *_sharedNetworkQueue;
Expand All @@ -54,6 +55,7 @@ @implementation MKNetworkEngine
@synthesize memoryCache = _memoryCache;
@synthesize memoryCacheKeys = _memoryCacheKeys;


// Network Queue is a shared singleton object.
// no matter how many instances of MKNetworkEngine is created, there is one and only one network queue
// In theory an app should contain as many network engines as the number of domains it talks to
Expand Down Expand Up @@ -131,6 +133,7 @@ -(void) reachabilityChanged:(NSNotification*) notification
{
DLog(@"Server [%@] is reachable via Wifi", self.hostName);
[_sharedNetworkQueue setMaxConcurrentOperationCount:6];

[self checkAndRestoreFrozenOperations];
}
else if([self.reachability currentReachabilityStatus] == ReachableViaWWAN)
Expand All @@ -151,6 +154,8 @@ -(void) reachabilityChanged:(NSNotification*) notification
#pragma Freezing operations (Called when network connectivity fails)
-(void) freezeOperations {

if(![self isCacheEnabled]) return;

for(MKNetworkOperation *operation in _sharedNetworkQueue.operations) {

if(![operation freezable]) continue; // freeze only freeable operations.
Expand All @@ -165,6 +170,8 @@ -(void) freezeOperations {

-(void) checkAndRestoreFrozenOperations {

if(![self isCacheEnabled]) return;

NSError *error = nil;
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self cacheDirectoryName] error:&error];
if(error)
Expand Down Expand Up @@ -371,6 +378,13 @@ - (BOOL) dataOldness:(NSString*) imagePath
return abs([creationDate timeIntervalSinceNow]);
}*/

-(BOOL) isCacheEnabled {

BOOL isDir = NO;
BOOL isCachingEnabled = [[NSFileManager defaultManager] fileExistsAtPath:[self cacheDirectoryName] isDirectory:&isDir];
return isCachingEnabled;
}

-(void) useCache {

self.memoryCache = [NSMutableDictionary dictionaryWithCapacity:[self cacheMemoryCost]];
Expand Down
3 changes: 3 additions & 0 deletions MKNetworkKit/MKNetworkKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@

#define MKNETWORKCACHE_DEFAULT_COST 10
#define MKNETWORKCACHE_DEFAULT_DIRECTORY @"MKNetworkKitCache"

#endif


1 change: 1 addition & 0 deletions MKNetworkKit/MKNetworkOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// THE SOFTWARE.

#import <UIKit/UIKit.h>
#import "MKNetworkKit.h"

/*!
@header MKNetworkOperation.h
Expand Down
5 changes: 0 additions & 5 deletions README.mdown
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,3 @@ I'll be working on this soon. But you can start forking it and getting it right

###A note on licensing
Attribution free licensing available upon request. Contact me at <a href="mailto:[email protected]"> [email protected] </a>

Change MKSG twitter password
Cleanup download paths


0 comments on commit dc4aa45

Please sign in to comment.