4

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 issue is that after I get the initial server response, say with 1 league that has 10 games - if I delete a game and re-fetch the league from the server the orphaned object doesn't get deleted from Core Data. Using my fetch request results in all games being deleted.

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

What am I missing here?

Edit: I was able to get the objects to map using an answer here Now I just need to get the orphaned objects to delete but the fetch request isn't working

Bryan
  • 3,199
  • 3
  • 16
  • 24

0 Answers0