mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-01-12 22:51:50 +08:00
Ensure the fetch request blocks are given a relative URL if possible to make path matching easier
This commit is contained in:
@@ -154,10 +154,18 @@
|
||||
- (NSSet *)localObjectsFromFetchRequestsMatchingRequestURL:(NSError **)error
|
||||
{
|
||||
NSMutableSet *localObjects = [NSMutableSet set];
|
||||
NSURL *URL = [self.request URL];
|
||||
__block NSError *_blockError;
|
||||
__block NSArray *_blockObjects;
|
||||
|
||||
// Pass the fetch request blocks a relative `NSURL` object if possible
|
||||
NSURL *URL = [self.request URL];
|
||||
NSArray *baseURLs = [self.responseDescriptors valueForKeyPath:@"@distinctUnionOfObjects.baseURL"];
|
||||
if ([baseURLs count] == 1) {
|
||||
NSURL *baseURL = baseURLs[0];
|
||||
NSString *pathAndQueryString = RKPathAndQueryStringFromURLRelativeToURL(URL, baseURL);
|
||||
URL = [NSURL URLWithString:pathAndQueryString relativeToURL:baseURL];
|
||||
}
|
||||
|
||||
for (RKFetchRequestBlock fetchRequestBlock in [self.fetchRequestBlocks reverseObjectEnumerator]) {
|
||||
NSFetchRequest *fetchRequest = fetchRequestBlock(URL);
|
||||
if (fetchRequest) {
|
||||
|
||||
@@ -7,6 +7,11 @@
|
||||
//
|
||||
|
||||
#import "RKTestEnvironment.h"
|
||||
#import "RKManagedObjectRequestOperation.h"
|
||||
|
||||
@interface RKManagedObjectRequestOperation ()
|
||||
- (NSSet *)localObjectsFromFetchRequestsMatchingRequestURL:(NSError **)error;
|
||||
@end
|
||||
|
||||
@interface RKManagedObjectRequestOperationTest : RKTestCase
|
||||
@end
|
||||
@@ -18,4 +23,26 @@
|
||||
|
||||
}
|
||||
|
||||
- (void)testFetchRequestBlocksAreInvokedWithARelativeURL
|
||||
{
|
||||
NSURL *baseURL = [NSURL URLWithString:@"http://restkit.org/api/v1/"];
|
||||
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"categories/1234" relativeToURL:baseURL]];
|
||||
RKObjectMapping *mapping = [RKObjectMapping requestMapping];
|
||||
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:@"categories/:categoryID" keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:200]];
|
||||
responseDescriptor.baseURL = baseURL;
|
||||
RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
|
||||
__block NSURL *blockURL = nil;
|
||||
RKFetchRequestBlock fetchRequesBlock = ^NSFetchRequest *(NSURL *URL) {
|
||||
blockURL = URL;
|
||||
return nil;
|
||||
};
|
||||
|
||||
operation.fetchRequestBlocks = @[fetchRequesBlock];
|
||||
NSError *error;
|
||||
[operation localObjectsFromFetchRequestsMatchingRequestURL:&error];
|
||||
expect(blockURL).notTo.beNil();
|
||||
expect([blockURL.baseURL absoluteString]).to.equal(@"http://restkit.org/api/v1/");
|
||||
expect(blockURL.relativePath).to.equal(@"categories/1234");
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user