Created
January 24, 2012 08:17
-
-
Save blackgold9/1668813 to your computer and use it in GitHub Desktop.
modifications to enqueueOperation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void) enqueueOperation:(MKNetworkOperation*) operation forceReload:(BOOL) forceReload { | |
dispatch_queue_t originalQueue = dispatch_get_current_queue(); | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
[operation setCacheHandler:^(MKNetworkOperation* completedCacheableOperation) { | |
// if this is not called, the request would have been a non cacheable request | |
//completedCacheableOperation.cacheHeaders; | |
NSString *uniqueId = [completedCacheableOperation uniqueIdentifier]; | |
[self saveCacheData:[completedCacheableOperation responseData] | |
forKey:uniqueId]; | |
[self.cacheInvalidationParams setObject:completedCacheableOperation.cacheHeaders forKey:uniqueId]; | |
}]; | |
double expiryTimeInSeconds = 0.0f; | |
if(!forceReload) { | |
NSData *cachedData = [self cachedDataForOperation:operation]; | |
if(cachedData) { | |
dispatch_async(originalQueue, ^{ | |
[operation setCachedData:cachedData]; | |
}); | |
NSString *uniqueId = [operation uniqueIdentifier]; | |
NSMutableDictionary *savedCacheHeaders = [self.cacheInvalidationParams objectForKey:uniqueId]; | |
// there is a cached version. | |
// this means, the current operation is a "GET" | |
if(savedCacheHeaders) { | |
NSString *expiresOn = [savedCacheHeaders objectForKey:@"Expires"]; | |
NSDate *expiresOnDate = [NSDate dateFromRFC1123:expiresOn]; | |
expiryTimeInSeconds = [expiresOnDate timeIntervalSinceNow]; | |
[operation updateOperationBasedOnPreviousHeaders:savedCacheHeaders]; | |
} | |
} | |
} | |
NSUInteger index = [_sharedNetworkQueue.operations indexOfObject:operation]; | |
if(index == NSNotFound) { | |
if(expiryTimeInSeconds <= 0) | |
[_sharedNetworkQueue addOperation:operation]; | |
else if(forceReload) | |
[_sharedNetworkQueue addOperation:operation]; | |
// else don't do anything | |
} | |
else { | |
// This operation is already being processed | |
MKNetworkOperation *queuedOperation = (MKNetworkOperation*) [_sharedNetworkQueue.operations objectAtIndex:index]; | |
[queuedOperation updateHandlersFromOperation:operation]; | |
} | |
if([self.reachability currentReachabilityStatus] == NotReachable) | |
[self freezeOperations]; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment