LetzFlow
LetzFlow

Reputation: 481

Using RestKit 0.25 to parse local XML into CoreData

I'm trying to use RestKit to parse a local XML file into CoreData. I've done this successfully with online REST services, but parsing a local file or just parsing XML seems to be a completely different beast.

First I use a class named CoreDataHandler, which just encapsulates all the default CoreData stuff. So it's basically what xcode provides you with in the AppDelegate, put into a separate class.

What I've done so far:

CoreDataHandler* handler = [CoreDataHandler instance];

RKManagedObjectMappingOperationDataSource* dataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:handler.managedObjectContext cache:sharedManager.managedObjectStore.managedObjectCache];
dataSource.operationQueue = [NSOperationQueue new];

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"Seed_010000" ofType:@"xml"];
NSData *sourceObject = [NSData dataWithContentsOfFile:filePath];

RKObjectMapping* mapping = [self mapCourses];
RKMappingOperation* operation = [[RKMappingOperation alloc] initWithSourceObject:sourceObject destinationObject:nil mapping:mapping];
operation.dataSource = dataSource;

NSError* error = nil;
[operation performMapping:&error];

if( error == nil ) {
    NSLog(@"%@", [operation mappingInfo]);
    [handler saveContext];
} else {
    NSLog(@"error: %@", error);
}

The mappings are created here:

+ (RKObjectMapping*) mapCourses {
    RKEntityMapping *courseMapping = [RKEntityMapping mappingForClass:[Course class]];
    [courseMapping addAttributeMappingsFromDictionary:@{ @"Name.text" : @"name" }];

    RKEntityMapping* sectionMapping = [RKEntityMapping mappingForClass:[Section class]];
    [sectionMapping addAttributeMappingsFromDictionary:@{ @"order" : @"order",
                                                      @"Icon.text" : @"icon",
                                                      @"Name.text" : @"name",
                                                      @"Description.text" : @"desc" }];
    [courseMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Sections" toKeyPath:@"sections" withMapping:sectionMapping]];

    return courseMapping;
}

The XML File looks like this:

<MyProject>
  <Courses>
    <Course>
      <Name>Name of the Course</Name>
      <Sections>
        <Section order="0">
          <Icon>ICON_00</Icon>
          <Name>Name of the Section</Name>
          <Description>Description of the Section</Description>
        </Section>
      </Sections>
    </Course>
  </Courses>
</MyProject>

And the error I get is this:

2015-10-20 12:01:37.515 Training[16058:5400082] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSConcreteData 0x7fdf39f2b650> valueForUndefinedKey:]: this class is not key value coding-compliant for the key Sections.'

The stack trace points at the [operation performMaping:&error]; line.

I hope anybody can shed some light on this.

EDIT: I changed the question, because the first error (about the wrong initialiser was due to my own stupidity. I used RKObjectMapping instead of RKEntityMapping, which you have to use when using CoreData.

Upvotes: 0

Views: 51

Answers (1)

Wain
Wain

Reputation: 119031

You look to have a couple of problems. It seems that you're using 2 different managed object contexts, one from the rest kit stack and one from your own stack. You shouldn't be creating any context of your own.

For your error, it's because you're using RKObjectMapping where you should be using RKEntityMapping because you're using Core Data.

Upvotes: 1

Related Questions