Skip to content

Instantly share code, notes, and snippets.

@Catfish-Man
Created March 10, 2017 21:15
Show Gist options
  • Select an option

  • Save Catfish-Man/954a4b7a65bb5c7e03af57cac36d16d0 to your computer and use it in GitHub Desktop.

Select an option

Save Catfish-Man/954a4b7a65bb5c7e03af57cac36d16d0 to your computer and use it in GitHub Desktop.
static id cachedObject;
static NSLock *lock;
id getCachedObject() {
[lock lock];
id result = [cachedObject retain];
[lock unlock];
return result;
}
void setCachedObject(id newObject) {
[lock lock];
[cachedObject release];
cachedObject = [newObject retain];
[lock unlock];
}
//then in client code…
id object = getCachedObject();
[object doSomething];
[object release]; //not locked
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment