MKSG-MugunthKumar / MKNetworkKit

Modern NSURLSession based Networking Framework with built in authentication and HTTP 1.1 caching standards support for iOS 8+ devices
http://mk.sg/8w
3.06k stars 755 forks source link

Crash when reading cached data while max open file reached #385

Open genevictor opened 11 years ago

genevictor commented 11 years ago

An uncaught exception 'NSInvalidArgumentException', reason: '*\ setObjectForKey: object cannot be nil occasionally happen in MKNetworkEngine.m at line 619: (self.memoryCache)[cacheDataKey] = data;

In my case in the console usually come with:

: Max open files: 78 A bit of research shows that there exists a number of opened file limitation on iOS Further investigation locates the cause as follow: In MKNetworkEngine.m method cachedDataForOperation:(MKNetworkOperation*) operation: ``` if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { cachedData = [NSData dataWithContentsOfFile:filePath]; [self saveCacheData:cachedData forKey:[operation uniqueIdentifier]]; // bring it back to the in-memory cache return cachedData; } ``` line 394 can return cachedData with nil value Adding a simple if-condition check can avoid the problem.