I wrote a copied project example that hides the context from user-defined model classes: branch 10583736 .
(this is not the final production code, just a quick example, do not expect it to run into multi-threaded or strange errors)
Hiding the context for custom classes is simply defining custom methods to solve any situation where you usually request a context and use it.
You can define a class for the storage tier without exposing the context :
@interface DataStore : NSObject
+ (id)shared;
- (void)saveAll;
- (NSEntityDescription *)entityNamed:(NSString *)name;
- (NSManagedObject *)fetchEntity:(NSEntityDescription *)entity withPredicate:(NSPredicate *)predicate;
@end
, . , DataStore. .
@interface DataObject : NSManagedObject
+ (NSString *)entityName;
+ (NSEntityDescription *)entity;
- (void)save;
@end
, , , , , :
@interface Card : DataObject
@property (nonatomic, retain) NSString * question;
@property (nonatomic, retain) NSString * answer;
@property (nonatomic, retain) Deck *deck;
+ (Card *)card;
@end
- , .