Last active
December 11, 2021 15:32
-
-
Save iljaiwas/e5214ada50b4f9df7787875ebb661c21 to your computer and use it in GitHub Desktop.
Example how to build a class for iwascoding's Objective-C ORM system
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
// GSSavedAttributeSet.h | |
#import "SDBObject.h" | |
#import <SDBSynch/SDBSynchProtocols.h> | |
NS_ASSUME_NONNULL_BEGIN | |
@class GSEbayItemSpecific; | |
/** A named set of pre-filled listing attributes, which is specific to a certain eBay site and possibly categories */ | |
@interface GSSavedAttributeSet : SDBObject <SDBSynchContainer> | |
- (instancetype) initWithContext:(SDBContext*) inContext | |
name:(NSString*) inName | |
attributes:(NSArray<GSEbayItemSpecific*>*) inAttributes | |
siteID:(NSInteger) inSiteID | |
primaryCategoryID:(NSInteger) inPrimaryCategoryID | |
secondaryCategoryID:(NSInteger) inSecondaryCategoryID; | |
@property (readonly) NSString* name; | |
@property (readonly) NSArray<GSEbayItemSpecific*>* attributes; | |
@property (readonly) NSInteger siteID; | |
@property (readonly) NSInteger primaryCategoryID; | |
@property (readonly) NSInteger secondaryCategoryID; | |
@end | |
NS_ASSUME_NONNULL_END | |
// GSSavedAttributeSet.m | |
#import "GSSavedAttributeSet.h" | |
#import <IHLevelDB/SDBContext+Fetching.h> | |
@implementation GSSavedAttributeSet | |
/** Retrieve all attribute sets specific to certain eBay site, ordered by their name. */ | |
+ (NSArray<GSSavedAttributeSet*>*) savedAttributeSetsForSiteID:(NSInteger) inSiteID inContext:(SDBContext*) inContext | |
{ | |
return [inContext fetchObjectsOfClass:GSSavedAttributeSet.class | |
matchingPredicate:[NSPredicate predicateWithFormat:@"inSiteID == %ld", inSiteID] | |
sortedByDescriptor:[NSSortDescriptor sortDescriptorWithKey:@"name" | |
ascending:NO]]; | |
} | |
/** Retrieve all attribute sets specific to certain eBay site, ordered by their name. */ | |
+ (GSSavedAttributeSet*) savedAttributeSetWithName:(NSString*) inName siteID:(NSInteger) inSiteID inContext:(SDBContext*) inContext | |
{ | |
return [[inContext fetchObjectsOfClass:GSSavedAttributeSet.class | |
matchingPredicate:[NSPredicate predicateWithFormat:@"name == %@ && inSiteID == %ld", inName, inSiteID]] | |
anyObject]; | |
} | |
/** Index keys for this class for faster fetching */ | |
+ (NSSet*) propertyNamesToIndex | |
{ | |
return [NSSet setWithObjects:@"name", @"siteID", nil]; | |
} | |
/** Prevent instance from being automatically deleted from database when they aren't referenced by any other object. */ | |
+ (BOOL) instancesShouldAutoDelete | |
{ | |
return NO; | |
} | |
- (instancetype) initWithContext:(SDBContext*) inContext | |
name:(NSString*) inName | |
attributes:(NSArray<GSEbayItemSpecific*>*) inAttributes | |
siteID:(NSInteger) inSiteID | |
pimaryCategoryID:(NSInteger) inPrimaryCategoryID | |
secondaryCategoryID:(NSInteger) inSecondaryCategoryID | |
{ | |
self = [super initWithContext:inContext]; | |
if (self) { | |
_name = inName; | |
_attributes = inAttributes; | |
_siteID = inSiteID; | |
_primaryCategoryID = inPrimaryCategoryID; | |
_secondaryCategoryID = inSecondaryCategoryID; | |
} | |
return self; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment