RestKit不会删除孤立的关系对象

时间:2018-01-12 22:55:01

标签: ios objective-c restkit-0.20

我有以下映射:

RKEntityMapping *dashboardTeam = [RKEntityMapping mappingForEntityForName:@"DashboardTeam" inManagedObjectStore:managedObjectStore];
[dashboardTeam setAssignsDefaultValueForMissingAttributes:NO];
[dashboardTeam setAssignsNilForMissingRelationships:YES];
[dashboardTeam setShouldMapRelationshipsIfObjectIsUnmodified:YES];
[dashboardTeam addAttributeMappingsFromArray:@[@"id", @"name"]];

RKEntityMapping *dashboardLeague = [RKEntityMapping mappingForEntityForName:@"DashboardLeague" inManagedObjectStore:managedObjectStore];
[dashboardLeague setAssignsDefaultValueForMissingAttributes:YES];
[dashboardLeague setAssignsNilForMissingRelationships:YES];
[dashboardLeague setShouldMapRelationshipsIfObjectIsUnmodified:YES];
[dashboardLeague addAttributeMappingsFromArray:@[@"id", @"name"]];
[dashboardTeam addRelationshipMappingWithSourceKeyPath:@"league" mapping:dashboardLeague];

RKEntityMapping *dashboardGame = [RKEntityMapping mappingForEntityForName:@"DashboardGame" inManagedObjectStore:managedObjectStore];
[dashboardGame addAttributeMappingsFromArray:@[@"id", @"name", @"away", @"home", @"startTime"]];
[dashboardGame setAssignsNilForMissingRelationships:YES];
[dashboardGame setAssignsDefaultValueForMissingAttributes:YES];
[dashboardGame setShouldMapRelationshipsIfObjectIsUnmodified:YES];

RKDynamicMapping *dynamicGameMapping = [RKDynamicMapping new];
[dynamicGameMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {
    RKObjectMapping *testListMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
    for (NSString *key in representation) {
        [testListMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:key toKeyPath:key withMapping:dashboardGame]];
    }
    [testListMapping setAssignsNilForMissingRelationships:YES];
    [testListMapping setAssignsDefaultValueForMissingAttributes:YES];
    return testListMapping;
}];
[dashboardGame addRelationshipMappingWithSourceKeyPath:@"league" mapping:dashboardLeague];

RKResponseDescriptor *teams = [RKResponseDescriptor responseDescriptorWithMapping:dashboardTeam method:RKRequestMethodGET pathPattern:@"/api/3/dashboard" keyPath:@"teams" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

RKResponseDescriptor *games = [RKResponseDescriptor responseDescriptorWithMapping:dynamicGameMapping method:RKRequestMethodGET pathPattern:@"/api/3/dashboard" keyPath:@"games" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[[RKObjectManager sharedManager] addResponseDescriptorsFromArray:@[teams, games]];

以下获取请求:

[self.rkObjectManager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {
    RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:DashData.pathPattern];

    NSDictionary *argsDict = nil;
    BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO  parsedArguments:&argsDict];
    if (match)
        {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"DashboardGame"
                                                  inManagedObjectContext:[RKObjectManager sharedManager].managedObjectStore.persistentStoreManagedObjectContext];
        [fetchRequest setEntity:entity];

        return fetchRequest;
        }

    return nil;
}];

我的问题是,在得到最初的服务器响应之后,比如1个联赛有10个游戏 - 如果我删除游戏并从服务器重新获取联盟,则孤立对象不会从Core中删除数据。使用我的获取请求会导致所有游戏被删除。

在我的核心数据模型中,我将删除规则设置为Nullify并尝试了所有其他规则,但我不知道如何解决这个问题,过去我曾使用过如上所述的简单获取请求,但它没有&#39 ;当它在一段关系中时似乎有用。

我在这里缺少什么?

编辑:我能够使用答案here获取要映射的对象 现在我只需要删除孤立对象,但获取请求不起作用

0 个答案:

没有答案