Skip to content

Commit

Permalink
Merge pull request #13 from matrix-org/memory_optimisation
Browse files Browse the repository at this point in the history
Memory optimisation
  • Loading branch information
manuroe committed Oct 9, 2015
2 parents fe34764 + 4cdd8ae commit 56bfc71
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
46 changes: 33 additions & 13 deletions MatrixSDK/JSONModels/MXJSONModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion MatrixSDK/Utils/MXHTTPClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 56bfc71

Please sign in to comment.