StoreForRequestResourcePath
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(id<ODataStore>)storeForRequestToResourcePath:(NSString *)resourcePath | |
{ | |
/* | |
First, test if mode is online- or offline-only anyway. | |
*/ | |
if (self.workingMode == WorkingModeOnline) { | |
return self.networkStore; | |
} else if (self.workingMode == WorkingModeOffline) { | |
return self.localStore; | |
} | |
/* | |
And if there are any defining requests to test. If no, may as well send online. | |
*/ | |
if (self.definingRequests.count < 1) { | |
return self.networkStore; | |
} | |
/* | |
Second, do a compare to see if the collection of the request matches the collection | |
of the defining requests. The principle here is that you can offline a collection, | |
or filter of a collection, and all requests will be executed against the db for that | |
collection. You should adjust the filters of your defining requests to support the | |
expected scope of requests for the user. | |
*/ | |
__block NSString * (^collectionName)(NSString *) = ^NSString * (NSString *string){ | |
NSString *relativeString = string; | |
/* | |
If the string is a fully-qualified URL, parse out just the relative resource path | |
*/ | |
if ([[relativeString substringToIndex:4] isEqualToString:@"http"]) { | |
NSURL *url = [NSURL URLWithString:relativeString]; | |
relativeString = [url lastPathComponent]; | |
} | |
/* | |
Trim parenthesis element from last path component | |
*/ | |
if ([relativeString rangeOfString:@"("].location != NSNotFound) { | |
relativeString = [relativeString substringToIndex:[relativeString rangeOfString:@"("].location]; | |
} | |
return [relativeString rangeOfString:@"?"].location != NSNotFound ? [relativeString substringToIndex:[relativeString rangeOfString:@"?"].location] : relativeString; | |
}; | |
NSString *resourcePathCollectionName = collectionName(resourcePath); | |
for (NSString *request in self.definingRequests) { | |
NSString *definingRequestCollectionName = collectionName(request); | |
if ((resourcePathCollectionName && definingRequestCollectionName) && [resourcePathCollectionName isEqualToString:definingRequestCollectionName]) { | |
return self.localStore; | |
} | |
} | |
/* | |
Last, the default will always be to fall back to the network store (online request). | |
This should cover Function Imports, and any requests which are not within the scope | |
of the defining request collections | |
*/ | |
return self.networkStore; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment