steipete / SDURLCache

URLCache subclass with on-disk cache support on iPhone/iPad. Forked for speed!
http://twitter.com/steipete
MIT License
598 stars 102 forks source link

Adding error handling when trying to unarchive an invalid archive #13

Closed tibr closed 12 years ago

tibr commented 12 years ago

[NSKeyedUnarchiver unarchiveObjectWithFile:] might throw an error that looks like this and results in a crash:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSKeyedUnarchiver initForReadingWithData:]: incomprehensible archive (0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30)'

I added a catch block to handle the error. If the file is not a valid archive the cached response is removed and nil is returned as response.

steipete commented 12 years ago

Good call. I recently wrote another project that uses NSKeyedUnarchiver and found out about this ugly un-cocoa-like behavior myself. There should be an NSError, not an exception; but we have do deal with it.