RestKit does not delete an orphan object

I have the following mapping:

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]];

And the following fetch request:

[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;
}];

My problem is that after receiving the initial server response, say with 1 league in which there are 10 games - if I delete the game and re-receive the league from the server, the lost object will not be deleted from Core Data. Using my fetch request deletes all games.

In my Core Data model, I have a deletion rule set in Nullify and tried all the others, but I'm not sure how to solve this, in the past I used simple fetch requests as above, but it doesn't seem to work when he is within a relationship.

What am I missing here?

: ,

+4

Source: https://habr.com/ru/post/1692122/


All Articles