Skip to content

Instantly share code, notes, and snippets.

@Adlai-Holler
Created November 17, 2016 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adlai-Holler/76f74a7e659996dcb688ecc901770ddb to your computer and use it in GitHub Desktop.
Save Adlai-Holler/76f74a7e659996dcb688ecc901770ddb to your computer and use it in GitHub Desktop.
A test to ensure that NSCache doesn't give up after multiple memory warnings.
- (void)testThatNSCacheDoesntGiveUp
{
NSCache *cache = [[NSCache alloc] init];
for (NSInteger i = 0; i < 5; i++) {
// Add a couple entries for good measure
[cache setObject:(id)kCFNull forKey:[NSUUID UUID]];
[cache setObject:(id)kCFNull forKey:[NSUUID UUID]];
[self expectationForNotification:UIApplicationDidReceiveMemoryWarningNotification object:nil handler:nil];
NSLog(@"Awaiting memory warning…");
[self waitForExpectationsWithTimeout:30 handler:nil];
}
// Now that we've sent two memory warnings, ensure the cache still works.
NSUUID *newKey = [NSUUID UUID];
[cache setObject:(id)kCFNull forKey:newKey];
XCTAssertNotNil([cache objectForKey:newKey]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment