From e7a426c201ce6974e25701a125143339cfee3668 Mon Sep 17 00:00:00 2001 From: "John P. Swensen" Date: Sat, 25 Jun 2016 16:22:23 -0700 Subject: [PATCH 1/2] Modified the LBModelRepository to allow filter responses with the include filter to interpret arrays correctly --- LoopBack/LBModel.h | 7 +++++++ LoopBack/LBModel.m | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/LoopBack/LBModel.h b/LoopBack/LBModel.h index d99c24a..66bde4d 100644 --- a/LoopBack/LBModel.h +++ b/LoopBack/LBModel.h @@ -90,4 +90,11 @@ */ - (LBModel *)modelWithDictionary:(NSDictionary *)dictionary; +/** + * Accesses the singleton dictionary of all model repositories + * + * @return A dictionary of model + */ ++ (NSMutableDictionary*) repositoriesDict; + @end diff --git a/LoopBack/LBModel.m b/LoopBack/LBModel.m index 3a5037c..4c6ebe3 100644 --- a/LoopBack/LBModel.m +++ b/LoopBack/LBModel.m @@ -82,6 +82,9 @@ - (instancetype)initWithClassName:(NSString *)name { if (!self.modelClass) { self.modelClass = [LBModel class]; } + + // Add this repository to the dictionary of all repositories + [LBModelRepository.repositoriesDict setObject:self forKey:name]; } return self; @@ -130,6 +133,16 @@ - (LBModel *)modelWithDictionary:(NSDictionary *)dictionary { else if (strncmp(type, "T@\"CLLocation\",", 15) == 0) { obj = [SLObject locationFromEncodedProperty:obj]; } + } else if ([obj isKindOfClass:[NSArray class]]) + { + LBModelRepository* repository = [LBModelRepository.repositoriesDict objectForKey:key]; + NSMutableArray* tmp = [NSMutableArray array]; + for (NSDictionary* dict in obj) + { + LBModel* model = [repository modelWithDictionary:dict]; + [tmp addObject:model]; + } + obj = tmp; } @try { @@ -145,4 +158,14 @@ - (LBModel *)modelWithDictionary:(NSDictionary *)dictionary { return model; } ++ (NSMutableDictionary *) repositoriesDict { + static NSMutableDictionary *g_modelsDict = nil; + if (g_modelsDict == nil) { + // create dict + g_modelsDict = [[NSMutableDictionary alloc] init]; + } + return g_modelsDict; +} + + @end From 030e78243db92f6c395f383b9c1d197ab1fcf542 Mon Sep 17 00:00:00 2001 From: "John P. Swensen" Date: Fri, 9 Sep 2016 09:19:30 -0700 Subject: [PATCH 2/2] Added a findWithFilterString method the the LBPersistedModel. For some reason, none of the nested Dictionary objects were doing queries correctly for AND and OR operations on WHERE. Adding this method allows me to test all filters using cURL and then use the exact same query string from within my application. --- LoopBack/LBPersistedModel.h | 4 ++++ LoopBack/LBPersistedModel.m | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/LoopBack/LBPersistedModel.h b/LoopBack/LBPersistedModel.h index 9f35db2..832ab35 100644 --- a/LoopBack/LBPersistedModel.h +++ b/LoopBack/LBPersistedModel.h @@ -109,4 +109,8 @@ typedef void (^LBPersistedModelFindOneSuccessBlock)(LBPersistedModel *model); success:(LBPersistedModelAllSuccessBlock)success failure:(SLFailureBlock)failure; +- (void)findWithFilterString:(NSString *) filter + success: (LBPersistedModelAllSuccessBlock)success + failure:(SLFailureBlock)failure; + @end diff --git a/LoopBack/LBPersistedModel.m b/LoopBack/LBPersistedModel.m index b5fd51d..cdf4167 100644 --- a/LoopBack/LBPersistedModel.m +++ b/LoopBack/LBPersistedModel.m @@ -155,6 +155,26 @@ - (void)findWithFilter:(NSDictionary *) filter failure:failure]; } +- (void)findWithFilterString:(NSString *) filter + success: (LBPersistedModelAllSuccessBlock)success + failure:(SLFailureBlock)failure { + if(!filter) { + filter = @{}; + } + [self invokeStaticMethod:@"find" + parameters:@{@"filter": filter} + success:^(id value) { + NSAssert([[value class] isSubclassOfClass:[NSArray class]], @"Received non-Array: %@", value); + + NSMutableArray *models = [NSMutableArray array]; + [value enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { + [models addObject:[self modelWithDictionary:obj]]; + }]; + + success(models); + } + failure:failure]; +} @end