Last active
August 29, 2015 14:16
-
-
Save joshdholtz/be605e3dbed9d9365ff9 to your computer and use it in GitHub Desktop.
RLMObject+Additions
This file contains hidden or 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
// | |
// FloorModel.h | |
// | |
// Created by Josh Holtz on 3/4/15. | |
// Copyright (c) 2015 RokkinCat. All rights reserved. | |
// | |
#import "RLMObject.h" | |
@interface FloorModel : RLMObject | |
@property (nonatomic, assign) NSInteger ID; | |
@property NSString *blueprint; | |
@property NSString *name; | |
@property NSInteger orderIndex; | |
@property NSInteger buildingId; | |
+ (PMKPromise*)get:(NSDictionary*)params; | |
@end |
This file contains hidden or 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
// | |
// FloorModel.m | |
// | |
// Created by Josh Holtz on 3/4/15. | |
// Copyright (c) 2015 RokkinCat. All rights reserved. | |
// | |
#import "FloorModel.h" | |
#import "RLMObject+Additions.h" | |
#import "APIManager.h" | |
#import <Realm/Realm.h> | |
@implementation FloorModel | |
+ (NSString *)primaryKey { | |
return @"ID"; | |
} | |
RLM_REGISTER_KEYS( (@{ @"id" : @"ID", @"order_index" : @"orderIndex", @"building_id" : @"buildingId" }) ) | |
#pragma mark - API | |
+ (PMKPromise*)get:(NSDictionary*)params { | |
return [[APIManager sharedInstance] GET:@"floors" parameters:params].then(^(NSArray *responseObject, AFHTTPRequestOperation *operation){ | |
RLMResults *results = [self add_createOrUpdateInDefaultRealmWithObjects:responseObject andDeleteOthers:YES]; | |
return PMKManifold( results ); | |
}); | |
} | |
@end |
This file contains hidden or 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
// | |
// RLMObject+Additions.h | |
// | |
// Created by Josh Holtz on 3/4/15. | |
// Copyright (c) 2015 RokkinCat. All rights reserved. | |
// | |
#import "RLMObject.h" | |
@class RLMRealm; | |
@interface RLMObject (Additions) | |
+ (void)add_registerKeysToMap:(NSDictionary*)map; | |
+ (NSDictionary*)add_mapKeys:(NSDictionary*)object; | |
+ (instancetype)add_createInRealm:(RLMRealm *)realm withObject:(id)object; | |
+ (instancetype)add_createInDefaultRealmWithObject:(id)object; | |
+ (instancetype)add_createOrUpdateInDefaultRealmWithObject:(id)object; | |
+ (instancetype)add_createOrUpdateInRealm:(RLMRealm *)realm withObject:(id)object; | |
+ (RLMResults*)add_createOrUpdateInDefaultRealmWithObjects:(NSArray*)objects; | |
+ (RLMResults*)add_createOrUpdateInDefaultRealmWithObjects:(NSArray*)objects andDeleteOthers:(BOOL)deleteOthers; | |
+ (RLMResults*)add_createOrUpdateInRealm:(RLMRealm*)realm withObjects:(NSArray*)objects; | |
+ (RLMResults*)add_createOrUpdateInRealm:(RLMRealm*)realm withObjects:(NSArray*)objects andDeleteOthers:(BOOL)deleteOthers; | |
@end | |
#define RLM_REGISTER_KEYS(MAP) \ | |
+ (void) initialize { \ | |
[self add_registerKeysToMap:MAP]; \ | |
} |
This file contains hidden or 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
// | |
// RLMObject+Additions.m | |
// | |
// Created by Josh Holtz on 3/4/15. | |
// Copyright (c) 2015 RokkinCat. All rights reserved. | |
// | |
#import "RLMObject+Additions.h" | |
#import <objc/runtime.h> | |
#import <Realm/Realm.h> | |
@implementation RLMObject (Additions) | |
#pragma mark - Cleans | |
static void *MappingKey; | |
+ (void)add_registerKeysToMap:(NSDictionary*)map { | |
if (objc_getAssociatedObject(self, &MappingKey)) return; | |
objc_setAssociatedObject(self, &MappingKey, map, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
+ (NSDictionary*)add_mapKeys:(NSDictionary*)object { | |
NSMutableDictionary *mutObject = object.mutableCopy; | |
NSDictionary *maps = objc_getAssociatedObject(self, &MappingKey); | |
for (NSString *key in maps.allKeys) { | |
NSString *mapToKey = maps[key]; | |
id valueToMap = mutObject[key]; | |
if (mapToKey && valueToMap) mutObject[mapToKey] = valueToMap; | |
} | |
return mutObject; | |
} | |
#pragma mark - Creates | |
+ (instancetype)add_createInRealm:(RLMRealm *)realm withObject:(id)object { | |
return [self createInRealm:realm withObject:[self add_mapKeys:object]]; | |
} | |
+ (instancetype)add_createInDefaultRealmWithObject:(id)object { | |
return [self createInDefaultRealmWithObject:[self add_mapKeys:object]]; | |
} | |
+ (instancetype)add_createOrUpdateInDefaultRealmWithObject:(id)object { | |
return [self createOrUpdateInDefaultRealmWithObject:[self add_mapKeys:object]]; | |
} | |
+ (instancetype)add_createOrUpdateInRealm:(RLMRealm *)realm withObject:(id)object { | |
return [self createOrUpdateInRealm:realm withObject:[self add_mapKeys:object]]; | |
} | |
+ (RLMResults*)add_createOrUpdateInDefaultRealmWithObjects:(NSArray*)objects { | |
return [self add_createOrUpdateInRealm:[RLMRealm defaultRealm] withObjects:objects andDeleteOthers:NO]; | |
} | |
+ (RLMResults*)add_createOrUpdateInDefaultRealmWithObjects:(NSArray*)objects andDeleteOthers:(BOOL)deleteOthers { | |
return [self add_createOrUpdateInRealm:[RLMRealm defaultRealm] withObjects:objects andDeleteOthers:deleteOthers]; | |
} | |
+ (RLMResults*)add_createOrUpdateInRealm:(RLMRealm*)realm withObjects:(NSArray*)objects { | |
return [self add_createOrUpdateInRealm:realm withObjects:objects andDeleteOthers:NO]; | |
} | |
+ (RLMResults*)add_createOrUpdateInRealm:(RLMRealm*)realm withObjects:(NSArray*)objects andDeleteOthers:(BOOL)deleteOthers { | |
NSMutableSet *ids = [NSMutableSet set]; | |
// Save building data | |
[realm beginWriteTransaction]; | |
[objects enumerateObjectsUsingBlock:^(NSDictionary* item, NSUInteger idx, BOOL *stop) { | |
RLMObject *obj = [self createOrUpdateInRealm:realm withObject:[self add_mapKeys:item]]; | |
[ids addObject:obj[self.primaryKey]]; | |
}]; | |
RLMResults *toDelete = [self objectsInRealm:realm withPredicate:[NSPredicate predicateWithFormat:@"NOT (ID IN %@)", ids]]; | |
[realm deleteObjects:toDelete]; | |
[realm commitWriteTransaction]; | |
return [self objectsInRealm:realm withPredicate:[NSPredicate predicateWithFormat:@"ID IN %@", ids]]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment