RestKit不会从本地存储中删除孤立对象 [英] RestKit not deleting orphaned objects from local store

查看:97
本文介绍了RestKit不会从本地存储中删除孤立对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将RestKit从0.10.2更新到了0.20.3. 当Web服务中缺少对象时,立即更新后,RestKit不会从本地存储中删除它们.我知道RestKit 0.20.x支持它,但是我无法对其进行配置.我按照此处给出的示例进行操作. http://restkit.org/api/latest/Classes/RKManagedObjectRequestOperation.html#overview

Hi i have updated RestKit from 0.10.2 to 0.20.3. After updating now when objects are missing from web service, RestKit not deleting them from local storage. I know it is supported in RestKit 0.20.x, but i am not being able to configure it. I followed the example given here. http://restkit.org/api/latest/Classes/RKManagedObjectRequestOperation.html#overview

我在Stackoverflow上也遵循了类似的问题.但是还没有公认的答案.

I also followed similar questions on Stackoverflow. But there is no accepted answer yet.

这是我的杰森

{
    "status": "Success",
    "member_info": [
        {
            "family_id": "1",
            "user_id": "1"
        },
        {
            "family_id": "2",
            "user_id": "1"
        },
        {
            "family_id": "3",
            "user_id": "1"
        }
    ]
}

哪个返回特定用户的所有Family成员.在Json上方是user_id = 1.现在,我想如果请求发送给user_id = 2,那么应该从本地存储中删除所有前面的3个对象,因为Json现在缺少了它们.这是user_id = 2

Which returns all Family members for a particular user. Above Json is for user_id = 1. Now i want if request is sent for user_id = 2, then all the previous 3 objects should be deleted from local storage, as they are now missing from Json. Here is the new Json for user_id = 2

{
    "status": "Success",
    "member_info": [
        {
            "family_id": "4",
            "user_id": "2"
        },
        {
            "family_id": "5",
            "user_id": "2"
        }        
    ]
}

这就是我创建映射的方式.

This is how i am creating mapping.

[objectManager addResponseDescriptorsFromArray:@[                                                                                                                  
                                                     [RKResponseDescriptor responseDescriptorWithMapping:[self connectionsMapping]
                                                                                                  method:RKRequestMethodPOST
                                                                                             pathPattern:@"familylist"
                                                                                                 keyPath:@"member_info" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];



[objectManager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {
        RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"familylist"];
        NSDictionary *argsDict = nil;
        BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:&argsDict];
        if (match) {
            NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"DBConnections"];
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user_id == %@", [DBUser currentUser].user_id];
            fetchRequest.predicate = predicate;
            fetchRequest.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"first_name" ascending:YES] ];
            return fetchRequest;
        }

        return nil;
    }];


- (RKEntityMapping *)connectionsMapping {   
    RKEntityMapping *connectionsMapping = [RKEntityMapping mappingForEntityForName:@"DBConnections" inManagedObjectStore:objectManager.managedObjectStore];
    connectionsMapping.setDefaultValueForMissingAttributes = NO;
    connectionsMapping.identificationAttributes = @[@"family_id"];
    [connectionsMapping addAttributeMappingsFromDictionary:@{
                                                             @"family_id": @"family_id",
                                                             @"user_id": @"user_id",
                                                             }];

    return connectionsMapping;
}

在谓词中,我正在使用[DBUser currentUser].user_id,它将返回当前登录的user_id.这是我正在调用的Web服务

In predicate i am using [DBUser currentUser].user_id, which returns the current logged in user_id. And here is i am calling the web service

[[RKObjectManager sharedManager] postObject:nil path:@"familylist" parameters:params
                                            success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"Objects are %@", mappingResult.array);
}];

我使用的是postObject而不是getObjectsAtPath,因为服务器希望使用POST请求方法. 我做错了吗?或需要执行其他操作以删除孤立对象.感谢您的帮助.

I am using postObject instead of getObjectsAtPath, because server is expecting POST request method. Am i doing wrong? or need to do something else for deleting orphan objects. Any help is appreciated.

推荐答案

通过逐步调试RestKit最终解决了该问题,并在RKManagedObjectRequestOperation.m

Finally solved the problem by debugging RestKit step by step and found this code in RKManagedObjectRequestOperation.m

if (! [[self.HTTPRequestOperation.request.HTTPMethod uppercaseString] isEqualToString:@"GET"]) {
        RKLogDebug(@"Skipping deletion of orphaned objects: only performed for GET requests.");
        return YES;
    }

哦!当请求方法为POST时,RestKit不会删除孤立对象.我也更改了Web服务的accept GET请求,它运行良好. 其次,我正在使用predicate

Oh! RestKit doesn't delete orphan objects when the request method is POST. I changed the web services the accept GET request too and it worked perfectly. Secondly i was using predicate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user_id == %@", [DBUser currentUser].user_id];

这是错误的,要删除当前用户以外的所有对象,我必须像这样附加NOT运算符

It was wrong, to delete all objects except current user, i had to append NOT operator, like this

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user_id != %@", [DBUser currentUser].user_id];

所以predicate并不是您想要的,而是您不想要的.

So predicate doesn't mean what you want, but it is what you don't want.

这篇关于RestKit不会从本地存储中删除孤立对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆