Skip to content

Instantly share code, notes, and snippets.

@sstadelman
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sstadelman/e609a71b8e6c86bd8088 to your computer and use it in GitHub Desktop.
Save sstadelman/e609a71b8e6c86bd8088 to your computer and use it in GitHub Desktop.
StoreForRequestResourcePath
-(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