Restkit在核心数据映射之前截断db

时间:2013-10-29 15:19:55

标签: core-data restkit truncate restkit-0.20

我正在使用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;
}];

1 个答案:

答案 0 :(得分:0)

你的问题是

  

如果服务器没有发送任何内容,我不必做_any_thing

我猜测服务器仍然发送200响应而不是特殊状态代码(它应该是304 ‘Not Modified’)。因为如果你没有这个要求,你可以使用删除获取请求块(这是正确的解决方案)。

按照目前的情况,您可能必须自己通过从数据存储中提取现有项并迭代它们来检查项目是否在成功块中提供给您的映射结果中,如果不是,从上下文中删除它......