我正在使用restkit来映射一个带有核心数据的json
每次用户启动应用程序时,我都需要调用一个例程。
如果服务器更新了要发送的数据,我需要下载它们,截断我的表并将数据插入表中,如果服务器什么也没给我发送,我不必什么都不做。
显然,我只想在确定新数据已下载时才截断当前数据,而不是之前
我怎样才能做到这一点?
这是我的代码:
NSURL *endpoint = [NSURL URLWithString:kBaseURL];
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:endpoint];
[objectManager.HTTPClient setAuthorizationHeaderWithToken:@"username:my-token-12345"];
objectManager.managedObjectStore = [RKManagedObjectStore defaultStore];
[RKObjectManager setSharedManager:objectManager];
RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Medic" inManagedObjectStore:objectManager.managedObjectStore];
[entityMapping addAttributeMappingsFromDictionary:@{
@"identifier": @"identifier",
@"name": @"name",
@"surname": @"surname",
@"personalAddress": @"personalAddress",
@"hospital": @"hospital",
@"hospitalAddress": @"hospitalAddress",
@"oldDigitalAgreement": @"oldDigitalAgreement",
@"oldPaperAgreement": @"oldPaperAgreement"}];
entityMapping.identificationAttributes = @[@"identifier"];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor
responseDescriptorWithMapping:entityMapping
method:RKRequestMethodAny
pathPattern:nil
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
[[RKObjectManager sharedManager] getObjectsAtPath:kregistryURL
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
// done
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"attention", nil) message:NSLocalizedString(@"communication.genericerror.title", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
[alertView show];
}
];
Wain回答后的解决方案
[objectManager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {
RKPathMatcher* pathMatcher = [RKPathMatcher pathMatcherWithPattern:kregistryURL];
NSDictionary *dic = nil;
if ([pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:YES parsedArguments:&dic]) {
NSFetchRequest *fetchRequest = [NSFetchRequest new];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Medic"
inManagedObjectContext: [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext];
fetchRequest.entity = entity;
return fetchRequest;
} else
return nil;
}];