Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuru Taustahuzau committed Jul 21, 2014
2 parents be7c6ea + cb5c8df commit f6b3b85
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
0DDEB3A419641D6700F2461E /* RFWebServiceCachingModel_v3.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = RFWebServiceCachingModel_v3.xcdatamodel; sourceTree = "<group>"; };
1176413A18E46C0500B1688B /* libUtilities.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libUtilities.a; path = "../Utilities/build/Debug-iphoneos/libUtilities.a"; sourceTree = "<group>"; };
11FF622618BCE61700053F60 /* RFWebClientWithSharedHeader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RFWebClientWithSharedHeader.h; sourceTree = "<group>"; };
11FF622718BCE61700053F60 /* RFWebClientWithSharedHeader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RFWebClientWithSharedHeader.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 = "<group>";
versionGroupType = wrapper.xcdatamodel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ - (NSManagedObjectModel *)managedObjectModel {

- (NSManagedObjectContext *)managedObjectContext {
if (!_managedObjectContext) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:[self persisitentStoreCoordinator]];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<plist version="1.0">
<dict>
<key>_XCCurrentVersionName</key>
<string>RFWebServiceCachingModel_cacheIdentifierAdded.xcdatamodel</string>
<string>RFWebServiceCachingModel_v3.xcdatamodel</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model userDefinedModelVersionIdentifier="v1.1.2" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="3401" systemVersion="13B42" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
<model userDefinedModelVersionIdentifier="v1.1.2" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="5064" systemVersion="13D65" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
<entity name="WebResponse" representedClassName="RFWebResponse" syncable="YES">
<attribute name="cacheIdentifier" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="eTag" optional="YES" attributeType="String" syncable="YES"/>
Expand All @@ -12,6 +12,6 @@
<attribute name="urlHash" optional="YES" attributeType="Decimal" defaultValueString="0.0" syncable="YES"/>
</entity>
<elements>
<element name="WebResponse" positionX="0" positionY="0" width="0" height="0"/>
<element name="WebResponse" positionX="0" positionY="0" width="128" height="180"/>
</elements>
</model>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model userDefinedModelVersionIdentifier="v1.1.2" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="5064" systemVersion="13D65" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
<entity name="WebResponse" representedClassName="RFWebResponse" syncable="YES">
<attribute name="cacheIdentifier" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="eTag" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="expirationDate" optional="YES" attributeType="Date" syncable="YES"/>
<attribute name="lastModified" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="requestBodyData" optional="YES" attributeType="Binary" syncable="YES"/>
<attribute name="requestURL" attributeType="String" indexed="YES" syncable="YES"/>
<attribute name="response" attributeType="Binary" syncable="YES"/>
<attribute name="responseBodyData" optional="YES" attributeType="Binary" syncable="YES"/>
<attribute name="urlHash" attributeType="Decimal" defaultValueString="0.0" indexed="YES" syncable="YES"/>
</entity>
<elements>
<element name="WebResponse" positionX="0" positionY="0" width="128" height="178"/>
</elements>
</model>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model userDefinedModelVersionIdentifier="v1.1.2" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="3401" systemVersion="13B42" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
<entity name="WebResponse" representedClassName="RFWebResponse" syncable="YES">
<attribute name="cacheIdentifier" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="eTag" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="expirationDate" optional="YES" attributeType="Date" syncable="YES"/>
<attribute name="lastModified" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="requestBodyData" optional="YES" attributeType="Binary" syncable="YES"/>
<attribute name="requestURL" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="response" optional="YES" attributeType="Binary" syncable="YES"/>
<attribute name="responseBodyData" optional="YES" attributeType="Binary" syncable="YES"/>
<attribute name="urlHash" optional="YES" attributeType="Decimal" defaultValueString="0.0" syncable="YES"/>
</entity>
<elements>
<element name="WebResponse" positionX="0" positionY="0" width="0" height="0"/>
</elements>
</model>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit f6b3b85

Please sign in to comment.