diff --git a/MatrixSDK/JSONModels/MXJSONModel.m b/MatrixSDK/JSONModels/MXJSONModel.m index 6f3f5423b9..b192f4cc3b 100644 --- a/MatrixSDK/JSONModels/MXJSONModel.m +++ b/MatrixSDK/JSONModels/MXJSONModel.m @@ -21,23 +21,43 @@ @implementation MXJSONModel NSMutableDictionary *others; } -+ (NSDictionary *)JSONKeyPathsByPropertyKey -{ - NSMutableDictionary *JSONKeyPathsByPropertyKey = [NSMutableDictionary dictionary]; - - NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=[a-z])([A-Z])|([A-Z])(?=[a-z])" options:0 error:nil]; +/** + * The JSONKeyPathsByPropertyKey dictionnaries for all subclasses of MXJSONModel. + * The key is the child class name. The value, the JSONKeyPathsByPropertyKey dictionnary of the child class. + */ +static NSMutableDictionary *JSONKeyPathsByPropertyKeyByClass; - // List all properties defined by the class - NSSet *propertyKeys = [self.class propertyKeys]; - for (NSString *propertyKey in propertyKeys) ++ (void)initialize +{ + @synchronized(JSONKeyPathsByPropertyKeyByClass) { - // Manage camel-cased properties - // Home server uses underscore-separated compounds keys like "event_id". ObjC properties name trend is more camelCase like "eventId". - NSString *underscoredString = [[regex stringByReplacingMatchesInString:propertyKey options:0 range:NSMakeRange(0, propertyKey.length) withTemplate:@"_$1$2"] lowercaseString]; - JSONKeyPathsByPropertyKey[propertyKey] = underscoredString; + if (!JSONKeyPathsByPropertyKeyByClass) + { + JSONKeyPathsByPropertyKeyByClass = [NSMutableDictionary dictionary]; + } + + // Compute the JSONKeyPathsByPropertyKey for this subclass + NSMutableDictionary *JSONKeyPathsByPropertyKey = [NSMutableDictionary dictionary]; + + NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=[a-z])([A-Z])|([A-Z])(?=[a-z])" options:0 error:nil]; + + // List all properties defined by the class + NSSet *propertyKeys = [self.class propertyKeys]; + for (NSString *propertyKey in propertyKeys) + { + // Manage camel-cased properties + // Home server uses underscore-separated compounds keys like "event_id". ObjC properties name trend is more camelCase like "eventId". + NSString *underscoredString = [[regex stringByReplacingMatchesInString:propertyKey options:0 range:NSMakeRange(0, propertyKey.length) withTemplate:@"_$1$2"] lowercaseString]; + JSONKeyPathsByPropertyKey[propertyKey] = underscoredString; + } + + JSONKeyPathsByPropertyKeyByClass[NSStringFromClass(self.class)] = JSONKeyPathsByPropertyKey; } +} - return JSONKeyPathsByPropertyKey; ++ (NSDictionary *)JSONKeyPathsByPropertyKey +{ + return JSONKeyPathsByPropertyKeyByClass[NSStringFromClass(self.class)]; } + (id)modelFromJSON:(NSDictionary *)JSONDictionary diff --git a/MatrixSDK/Utils/MXHTTPClient.m b/MatrixSDK/Utils/MXHTTPClient.m index 079fa818f5..ec535394b0 100644 --- a/MatrixSDK/Utils/MXHTTPClient.m +++ b/MatrixSDK/Utils/MXHTTPClient.m @@ -189,9 +189,13 @@ - (void)tryRequest:(MXHTTPOperation*)mxHTTPOperation mxHTTPOperation.numberOfTries++; mxHTTPOperation.operation = [httpManager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, NSDictionary *JSONResponse) { + mxHTTPOperation.operation = nil; success(JSONResponse); - + } failure:^(AFHTTPRequestOperation *operation, NSError *error) { + + mxHTTPOperation.operation = nil; + #if DEBUG NSLog(@"[MXHTTPClient] Request %p failed for path: %@ - HTTP code: %ld", mxHTTPOperation, path, (long)operation.response.statusCode); #else