Created
April 16, 2015 23:56
-
-
Save pm-dev/07bd170def3706f56701 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// OPModel.h | |
// OperatoriOS | |
// | |
// Created by Peter Meyers on 4/16/15. | |
// Copyright (c) 2015 Peter Meyers. All rights reserved. | |
// | |
#import <CoreData/CoreData.h> | |
typedef void (^OPAsynchronousFetchResultsBlock)(NSArray *results); | |
typedef void (^OPAsynchronousFetchErrorBlock)(NSError *error); | |
@protocol OPModelProtocol <NSObject> | |
+ (instancetype)create; | |
+ (void)fetchID:(id)ID | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock; | |
+ (void)fetchID:(id)ID | |
refresh:(BOOL)forceRefresh | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock; | |
+ (void)fetchIDs:(NSOrderedSet *)IDs | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock; | |
+ (void)fetchIDs:(NSOrderedSet *)IDs | |
refresh:(BOOL)forceRefresh | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock; | |
+ (void)fetchIDs:(NSOrderedSet *)IDs | |
offset:(NSUInteger)offset | |
limit:(NSUInteger)limit | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock; | |
+ (void)fetchIDs:(NSOrderedSet *)IDs | |
offset:(NSUInteger)offset | |
limit:(NSUInteger)limit | |
refresh:(BOOL)forceRefresh | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock; | |
- (void)fetchRelationship:(SEL)relationshipSelector | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock; | |
- (void)fetchRelationship:(SEL)relationshipSelector | |
refresh:(BOOL)forceRefresh | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock; | |
- (void)fetchRelationship:(SEL)relationshipSelector | |
offset:(NSUInteger)offset | |
limit:(NSUInteger)limit | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock; | |
- (void)fetchRelationship:(SEL)relationshipSelector | |
offset:(NSUInteger)offset | |
limit:(NSUInteger)limit | |
refresh:(BOOL)forceRefresh | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock; | |
- (void)refresh:(OPAsynchronousFetchResultsBlock)successBlock error:(OPAsynchronousFetchErrorBlock)errorBlock; | |
- (void)sync:(OPAsynchronousFetchResultsBlock)successBlock error:(OPAsynchronousFetchErrorBlock)errorBlock; | |
- (void)delete:(OPAsynchronousFetchResultsBlock)successBlock error:(OPAsynchronousFetchErrorBlock)errorBlock; | |
@end | |
@interface OPModel : NSManagedObject <OPModelProtocol> | |
@end | |
// | |
// OPModel.m | |
// OperatoriOS | |
// | |
// Created by Peter Meyers on 4/16/15. | |
// Copyright (c) 2015 Peter Meyers. All rights reserved. | |
// | |
#import "OPModel.h" | |
#import "OPRESTCoreData.h" | |
@implementation OPModel | |
+ (instancetype) create | |
{ | |
return [OPRESTCoreData.sharedInstance createManagedObjectOfClass:self]; | |
} | |
+ (void)fetchID:(id)ID | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock | |
{ | |
[self fetchID:ID refresh:NO success:successBlock error:errorBlock]; | |
} | |
+ (void)fetchID:(id)ID | |
refresh:(BOOL)forceRefresh | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock | |
{ | |
if (ID) { | |
NSOrderedSet *IDs = [NSOrderedSet orderedSetWithObject:ID]; | |
[self fetchIDs:IDs offset:0 limit:0 refresh:forceRefresh success:successBlock error:errorBlock]; | |
} else if (successBlock) { | |
successBlock(@[]); | |
} | |
} | |
+ (void)fetchIDs:(NSOrderedSet *)IDs | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock | |
{ | |
[self fetchIDs:IDs refresh:NO success:successBlock error:errorBlock]; | |
} | |
+ (void)fetchIDs:(NSOrderedSet *)IDs | |
refresh:(BOOL)forceRefresh | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock | |
{ | |
[self fetchIDs:IDs offset:0 limit:0 refresh:forceRefresh success:successBlock error:errorBlock]; | |
} | |
+ (void)fetchIDs:(NSOrderedSet *)IDs | |
offset:(NSUInteger)offset | |
limit:(NSUInteger)limit | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock | |
{ | |
[self fetchIDs:IDs offset:offset limit:limit refresh:NO success:successBlock error:errorBlock]; | |
} | |
+ (void)fetchIDs:(NSOrderedSet *)IDs | |
offset:(NSUInteger)offset | |
limit:(NSUInteger)limit | |
refresh:(BOOL)forceRefresh | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock | |
{ | |
[OPRESTCoreData.sharedInstance fetchManagedObjectsOfClass:self IDs:IDs offset:offset limit:limit refresh:forceRefresh success:successBlock error:errorBlock]; | |
} | |
- (void)fetchRelationship:(SEL)relationshipSelector | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock | |
{ | |
[self fetchRelationship:relationshipSelector refresh:NO success:successBlock error:errorBlock]; | |
} | |
- (void)fetchRelationship:(SEL)relationshipSelector | |
refresh:(BOOL)forceRefresh | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock | |
{ | |
[self fetchRelationship:relationshipSelector offset:0 limit:0 refresh:forceRefresh success:successBlock error:errorBlock]; | |
} | |
- (void)fetchRelationship:(SEL)relationshipSelector | |
offset:(NSUInteger)offset | |
limit:(NSUInteger)limit | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock | |
{ | |
[self fetchRelationship:relationshipSelector offset:offset limit:limit refresh:NO success:successBlock error:errorBlock]; | |
} | |
- (void)fetchRelationship:(SEL)relationshipSelector | |
offset:(NSUInteger)offset | |
limit:(NSUInteger)limit | |
refresh:(BOOL)forceRefresh | |
success:(OPAsynchronousFetchResultsBlock)successBlock | |
error:(OPAsynchronousFetchErrorBlock)errorBlock | |
{ | |
NSString *relationshipName = NSStringFromSelector(relationshipSelector); | |
NSRelationshipDescription *relationship = self.entity.relationshipsByName[relationshipName]; | |
[OPRESTCoreData.sharedInstance fetchRelationship:relationship ofManagedObject:self offset:offset limit:limit refresh:forceRefresh success:successBlock error:errorBlock]; | |
} | |
- (void)refresh:(OPAsynchronousFetchResultsBlock)successBlock error:(OPAsynchronousFetchErrorBlock)errorBlock | |
{ | |
[[self class] fetchID:[OPRESTCoreData.sharedInstance IDForObject:self] refresh:YES success:successBlock error:errorBlock]; | |
} | |
- (void)sync:(OPAsynchronousFetchResultsBlock)successBlock error:(OPAsynchronousFetchErrorBlock)errorBlock | |
{ | |
if (self.hasChanges) { | |
[OPRESTCoreData.sharedInstance syncManagedObjects:[NSSet setWithObject:self] success:successBlock error:errorBlock]; | |
} else if (successBlock) { | |
successBlock(@[self]); | |
} | |
} | |
- (void)delete:(OPAsynchronousFetchResultsBlock)successBlock error:(OPAsynchronousFetchErrorBlock)errorBlock | |
{ | |
[self.managedObjectContext deleteObject:self]; | |
[self sync:successBlock error:errorBlock]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment