Skip to content

Instantly share code, notes, and snippets.

@nsforge
Created July 9, 2015 01:56
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 nsforge/bba2fb28f9d745ff2c33 to your computer and use it in GitHub Desktop.
Save nsforge/bba2fb28f9d745ff2c33 to your computer and use it in GitHub Desktop.
Related Object Lookup Pattern
@interface Person
// Derived from JSON
@property (readonly) NSString *personID;
@property (readonly) NSString *name;
@end
////////////////////////////////////////
@interface Company
// Set on object creation
@property (readonly, weak) PersonManager *personManager;
// Derived from JSON
@property (readonly) NSString *companyID;
@property (readonly) NSString *generalManagerPersonID;
// Convenience Methods
@property (readonly) Person *generalManager; // returns [self.personManager personForPersonID:self.generalManagerPersonID];
@end
////////////////////////////////////////
@interface PersonManager
@property (readonly) NSArray *persons;
// Initially, this will be a naive linear-search, requiring no extra state or configuration.
// If it turns out to be a performance hotspot, these query methods can be optimised by
// adding an NSDictionary cache (keyed by Person.personID), which will speed up all model-relation
// lookups that use the given method.
- (Person *)personForPersonID:(NSString *)personID;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment