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/RFWebServiceCacheContext.m b/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCacheContext.m index 63bd2d73..40ee990a 100644 --- a/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCacheContext.m +++ b/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCacheContext.m @@ -115,7 +115,7 @@ - (NSManagedObjectModel *)managedObjectModel { - (NSManagedObjectContext *)managedObjectContext { if (!_managedObjectContext) { - _managedObjectContext = [[NSManagedObjectContext alloc] init]; + _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; [_managedObjectContext setPersistentStoreCoordinator:[self persisitentStoreCoordinator]]; } diff --git a/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingManager.m b/Framework/ROADWebService/ROADWebService/Caching/RFWebServiceCachingManager.m index 30bb1397..eda808eb 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 * 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; + + } + + 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 if ([cacheIdentifier length]) { NSArray *responsesWithCacheId = [self unsafeFetchResponseForIdentifier:cacheIdentifier prefixed:NO]; for (RFWebResponse *cachedResponse in responsesWithCacheId) { - [managedObjectContext deleteObject:cachedResponse]; + if (cachedResponse.objectID != webResponse.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]); } @@ -137,7 +143,7 @@ - (RFWebResponse *)cacheWithRequest:(NSMutableURLRequest *)request { - (RFWebResponse *)cacheForResponse:(NSHTTPURLResponse *)response request:(NSURLRequest *)request cacheAttribute:(RFWebServiceCache *)cacheAttribute { RFWebResponse *cachedResponse = [self fetchResponseForRequest:request]; - if (!cacheAttribute.offlineCache && [response statusCode] != 304) { + if (!(cacheAttribute.offlineCache && !response) && [response statusCode] != 304) { if (cachedResponse) { [_cacheContext.context deleteObject:cachedResponse]; NSError *saveError; 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 diff --git a/Framework/ROADWebService/ROADWebService/Private/Downloader/RFDownloader.m b/Framework/ROADWebService/ROADWebService/Private/Downloader/RFDownloader.m index 859eaa4e..2d45a8d8 100644 --- a/Framework/ROADWebService/ROADWebService/Private/Downloader/RFDownloader.m +++ b/Framework/ROADWebService/ROADWebService/Private/Downloader/RFDownloader.m @@ -278,6 +278,7 @@ - (NSMutableURLRequest *)requestForUrl:(NSURL * const)anUrl withMethod:(NSString NSMutableURLRequest * const request = [NSMutableURLRequest requestWithURL:anUrl]; request.HTTPMethod = method; request.HTTPBody = body; + request.cachePolicy = NSURLRequestReloadIgnoringCacheData; return request; }