From 43ad0da78d3c7a198f2d085613964b88fc8ae196 Mon Sep 17 00:00:00 2001 From: Andrei Pitsko Date: Wed, 2 Jul 2014 16:55:56 +0300 Subject: [PATCH 1/3] Small performance optimizations ( cache Id and request URL was made as indexed field in DB, logic of saving cache was optimized) --- .../ROADWebService.xcodeproj/project.pbxproj | 4 +- .../Caching/RFWebServiceCachingManager.m | 38 +++++++++++-------- .../.xccurrentversion | 2 +- .../contents | 4 +- .../contents | 17 +++++++++ .../contents | 17 +++++++++ 6 files changed, 62 insertions(+), 20 deletions(-) create mode 100644 Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingModel.xcdatamodeld/RFWebServiceCachingModel_v3.xcdatamodel/contents create mode 100644 Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingModel.xcdatamodeld/RFWebServiceCachingModel_v3_optimizations.xcdatamodel/contents diff --git a/Framework/ROADWebService/ROADWebService.xcodeproj/project.pbxproj b/Framework/ROADWebService/ROADWebService.xcodeproj/project.pbxproj index f4300693..a2325f00 100644 --- a/Framework/ROADWebService/ROADWebService.xcodeproj/project.pbxproj +++ b/Framework/ROADWebService/ROADWebService.xcodeproj/project.pbxproj @@ -203,6 +203,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 0DDEB3A419641D6700F2461E /* RFWebServiceCachingModel_v3.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = RFWebServiceCachingModel_v3.xcdatamodel; sourceTree = ""; }; 1176413A18E46C0500B1688B /* libUtilities.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libUtilities.a; path = "../Utilities/build/Debug-iphoneos/libUtilities.a"; sourceTree = ""; }; 11FF622618BCE61700053F60 /* RFWebClientWithSharedHeader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RFWebClientWithSharedHeader.h; sourceTree = ""; }; 11FF622718BCE61700053F60 /* RFWebClientWithSharedHeader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RFWebClientWithSharedHeader.m; sourceTree = ""; }; @@ -1144,10 +1145,11 @@ 83138AF21862FFC700674184 /* RFWebServiceCachingModel.xcdatamodeld */ = { isa = XCVersionGroup; children = ( + 0DDEB3A419641D6700F2461E /* RFWebServiceCachingModel_v3.xcdatamodel */, 6338326D18AE6258005E1673 /* RFWebServiceCachingModel_cacheIdentifierAdded.xcdatamodel */, 83138AF31862FFC700674184 /* RFWebServiceCachingModel.xcdatamodel */, ); - currentVersion = 6338326D18AE6258005E1673 /* RFWebServiceCachingModel_cacheIdentifierAdded.xcdatamodel */; + currentVersion = 0DDEB3A419641D6700F2461E /* RFWebServiceCachingModel_v3.xcdatamodel */; path = RFWebServiceCachingModel.xcdatamodeld; sourceTree = ""; versionGroupType = wrapper.xcdatamodel; diff --git a/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingManager.m b/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingManager.m index 30bb1397..5478afd8 100644 --- a/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingManager.m +++ b/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingManager.m @@ -82,31 +82,37 @@ - (void)setCacheWithRequest:(NSURLRequest *)request response:(NSHTTPURLResponse RFWebServiceCachingManager *strongSelf = weakSelf; NSManagedObjectContext *managedObjectContext = strongSelf->_cacheContext.context; + RFWebResponse *rFWebResponse = [self unsafeFetchResponseForRequest:request]; + if (!rFWebResponse) { + rFWebResponse = [NSEntityDescription insertNewObjectForEntityForName:kRFWebResponseEntityName inManagedObjectContext:managedObjectContext]; + rFWebResponse.urlHash = [[NSDecimalNumber alloc] initWithUnsignedInteger:[[request.URL absoluteString] hash]]; + rFWebResponse.requestURL = [request.URL absoluteString]; + rFWebResponse.cacheIdentifier = cacheIdentifier; + + } + + rFWebResponse.requestBodyData = request.HTTPBody; + rFWebResponse.response = [NSKeyedArchiver archivedDataWithRootObject:response]; + rFWebResponse.responseBodyData = responseBodyData; + rFWebResponse.expirationDate = expirationDate; + rFWebResponse.eTag = eTag; + rFWebResponse.lastModified = lastModified; + + // Remove old one if exist if ([cacheIdentifier length]) { NSArray *responsesWithCacheId = [self unsafeFetchResponseForIdentifier:cacheIdentifier prefixed:NO]; for (RFWebResponse *cachedResponse in responsesWithCacheId) { - [managedObjectContext deleteObject:cachedResponse]; + if (cachedResponse.objectID != rFWebResponse.objectID) { + [managedObjectContext deleteObject:cachedResponse]; + } } } - RFWebResponse *oldCachedResponse = [self unsafeFetchResponseForRequest:request]; - if (oldCachedResponse) { - [managedObjectContext deleteObject:oldCachedResponse]; - } - - RFWebResponse *newWebResponse = [NSEntityDescription insertNewObjectForEntityForName:kRFWebResponseEntityName inManagedObjectContext:managedObjectContext]; - newWebResponse.urlHash = [[NSDecimalNumber alloc] initWithUnsignedInteger:[[request.URL absoluteString] hash]]; - newWebResponse.requestURL = [request.URL absoluteString]; - newWebResponse.requestBodyData = request.HTTPBody; - newWebResponse.response = [NSKeyedArchiver archivedDataWithRootObject:response]; - newWebResponse.responseBodyData = responseBodyData; - newWebResponse.expirationDate = expirationDate; - newWebResponse.eTag = eTag; - newWebResponse.lastModified = lastModified; - newWebResponse.cacheIdentifier = cacheIdentifier; + NSError *error; [managedObjectContext save:&error]; + if (error) { RFWSLogError(@"RFWebServiceCachingManager error: saving cached response failed with error: %@", [error localizedDescription]); } diff --git a/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingModel.xcdatamodeld/.xccurrentversion b/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingModel.xcdatamodeld/.xccurrentversion index a082d3f1..e5792fcd 100644 --- a/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingModel.xcdatamodeld/.xccurrentversion +++ b/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingModel.xcdatamodeld/.xccurrentversion @@ -3,6 +3,6 @@ _XCCurrentVersionName - RFWebServiceCachingModel_cacheIdentifierAdded.xcdatamodel + RFWebServiceCachingModel_v3.xcdatamodel diff --git a/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingModel.xcdatamodeld/RFWebServiceCachingModel_cacheIdentifierAdded.xcdatamodel/contents b/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingModel.xcdatamodeld/RFWebServiceCachingModel_cacheIdentifierAdded.xcdatamodel/contents index a7fb9fc5..d27642c1 100644 --- a/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingModel.xcdatamodeld/RFWebServiceCachingModel_cacheIdentifierAdded.xcdatamodel/contents +++ b/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingModel.xcdatamodeld/RFWebServiceCachingModel_cacheIdentifierAdded.xcdatamodel/contents @@ -1,5 +1,5 @@ - + @@ -12,6 +12,6 @@ - + \ No newline at end of file diff --git a/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingModel.xcdatamodeld/RFWebServiceCachingModel_v3.xcdatamodel/contents b/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingModel.xcdatamodeld/RFWebServiceCachingModel_v3.xcdatamodel/contents new file mode 100644 index 00000000..3594da7d --- /dev/null +++ b/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingModel.xcdatamodeld/RFWebServiceCachingModel_v3.xcdatamodel/contents @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingModel.xcdatamodeld/RFWebServiceCachingModel_v3_optimizations.xcdatamodel/contents b/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingModel.xcdatamodeld/RFWebServiceCachingModel_v3_optimizations.xcdatamodel/contents new file mode 100644 index 00000000..a7fb9fc5 --- /dev/null +++ b/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingModel.xcdatamodeld/RFWebServiceCachingModel_v3_optimizations.xcdatamodel/contents @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file From 68567aad88283ee611f878334e91ad3fc339c02a Mon Sep 17 00:00:00 2001 From: Andrei Pitsko Date: Mon, 7 Jul 2014 11:35:36 +0300 Subject: [PATCH 2/3] Prefix variable was removed --- .../Caching/RFWebServiceCachingManager.m | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingManager.m b/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingManager.m index 5478afd8..202b3ada 100644 --- a/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingManager.m +++ b/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingManager.m @@ -82,21 +82,21 @@ - (void)setCacheWithRequest:(NSURLRequest *)request response:(NSHTTPURLResponse RFWebServiceCachingManager *strongSelf = weakSelf; NSManagedObjectContext *managedObjectContext = strongSelf->_cacheContext.context; - RFWebResponse *rFWebResponse = [self unsafeFetchResponseForRequest:request]; - if (!rFWebResponse) { - rFWebResponse = [NSEntityDescription insertNewObjectForEntityForName:kRFWebResponseEntityName inManagedObjectContext:managedObjectContext]; - rFWebResponse.urlHash = [[NSDecimalNumber alloc] initWithUnsignedInteger:[[request.URL absoluteString] hash]]; - rFWebResponse.requestURL = [request.URL absoluteString]; - rFWebResponse.cacheIdentifier = cacheIdentifier; + RFWebResponse * webResponse = [self unsafeFetchResponseForRequest:request]; + if (!webResponse) { + webResponse = [NSEntityDescription insertNewObjectForEntityForName:kRFWebResponseEntityName inManagedObjectContext:managedObjectContext]; + webResponse.urlHash = [[NSDecimalNumber alloc] initWithUnsignedInteger:[[request.URL absoluteString] hash]]; + webResponse.requestURL = [request.URL absoluteString]; + webResponse.cacheIdentifier = cacheIdentifier; } - rFWebResponse.requestBodyData = request.HTTPBody; - rFWebResponse.response = [NSKeyedArchiver archivedDataWithRootObject:response]; - rFWebResponse.responseBodyData = responseBodyData; - rFWebResponse.expirationDate = expirationDate; - rFWebResponse.eTag = eTag; - rFWebResponse.lastModified = lastModified; + webResponse.requestBodyData = request.HTTPBody; + webResponse.response = [NSKeyedArchiver archivedDataWithRootObject:response]; + webResponse.responseBodyData = responseBodyData; + webResponse.expirationDate = expirationDate; + webResponse.eTag = eTag; + webResponse.lastModified = lastModified; // Remove old one if exist From 46200dd8d0c2ea576c946dfa27de727abe536896 Mon Sep 17 00:00:00 2001 From: Andrei Pitsko Date: Tue, 8 Jul 2014 13:31:57 +0300 Subject: [PATCH 3/3] Fixed building --- .../ROADWebService/Caching/RFWebServiceCachingManager.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingManager.m b/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingManager.m index 202b3ada..fd772f63 100644 --- a/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingManager.m +++ b/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingManager.m @@ -103,7 +103,7 @@ - (void)setCacheWithRequest:(NSURLRequest *)request response:(NSHTTPURLResponse if ([cacheIdentifier length]) { NSArray *responsesWithCacheId = [self unsafeFetchResponseForIdentifier:cacheIdentifier prefixed:NO]; for (RFWebResponse *cachedResponse in responsesWithCacheId) { - if (cachedResponse.objectID != rFWebResponse.objectID) { + if (cachedResponse.objectID != webResponse.objectID) { [managedObjectContext deleteObject:cachedResponse]; } }