2
votes

I installed RestKit (0.20.0-pre3) via CocoaPods and configured it as the documentation states it. When performing the getObjectsAtPath and RestKit does the mapping I get:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObjectContext saveToPersistentStore:]: unrecognized selector sent to instance 0x9eaae40'

If I set a breakpoint it stops at RKManagedObjectRequestOperation.m at line 278.

I configured RestKit as follows:

- (void)configureRestKit{

    //initialize RestKit
    LLog(DEBUG_LEVEL, @"init RestKit");

    NSString *baseURL;

    // Log all HTTP traffic with request and response bodies
    //RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);

    // Log debugging info about Core Data
    RKLogConfigureByName("RestKit/CoreData", RKLogLevelTrace);

    //Loging about the Entity Mapping
    RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace)

    // enable activity indicator
    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;

    if (local) {
        baseURL = @"http://xyz.net";

    } else {
        baseURL = @"http://XYZ.net";
    }


    [RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"application/json"];

    _objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:baseURL]];

    _managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
    _managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:_managedObjectModel];

    NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"XYZ.sqlite"];
    //LLog(DEBUG_MODE,@"The object store: %@", _objectStore.description);

    [_managedObjectStore addSQLitePersistentStoreAtPath:path fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:nil];

    [_managedObjectStore createManagedObjectContexts];
    [_managedObjectStore createPersistentStoreCoordinator];

    _managedObjectContext = _managedObjectStore.persistentStoreManagedObjectContext;
    _persistentStoreCoordinator = _managedObjectStore.persistentStoreCoordinator;
    _managedObjectModel = _managedObjectStore.managedObjectModel;

    _objectManager.managedObjectStore = _managedObjectStore;

    // 2012-06-13T00:00:00+01:00
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'+01:00'"];
    dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
    dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

    // Set it Globally
    [RKObjectMapping setPreferredDateFormatter:dateFormatter];

    LLog(DEBUG_LEVEL, @"DateFormater Test: %@", [dateFormatter stringFromDate:[NSDate date]]);

    // Add Entity Mapping
    RKEntityMapping *experienceMapping = [RKEntityMapping mappingForEntityForName:@"Experience" inManagedObjectStore:_managedObjectStore];
    [experienceMapping addAttributeMappingsFromArray:@[@"experienceid", @"city", @"descr", @"lat", @"lng", @"number", @"price", @"street"]];
    experienceMapping.identificationAttributes = @[@"experienceid"];


    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:experienceMapping pathPattern:@"/test.json" keyPath:@"experience" statusCodes:statusCodes];
    [_objectManager addResponseDescriptor:responseDescriptor];




    // ######  ONLY FOR TEST REASONS ######

    NSManagedObject *test = [NSEntityDescription
                                       insertNewObjectForEntityForName:@"Experience"
                                       inManagedObjectContext:_managedObjectContext];
    [test setValue:[NSNumber numberWithInt:1] forKey:@"experienceid"];
    [test setValue:@"Testville" forKey:@"city"];

    NSError *error;
    if (![_managedObjectContext save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }

    [_objectManager getObjectsAtPath:@"/test.json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
     // Handled with articleDescriptor
        LLog(DEBUG_LEVEL, @"Test");
     } failure:^(RKObjectRequestOperation *operation, NSError *error) {
     // Transport error or server error handled by errorDescriptor
     }];


}

Thanks for your help!

1
does no one have a clue? - Patrick

1 Answers

0
votes

This most likely indicates that your linker flags are not set up properly. The -ObjC linker flag is required to get category methods loaded. Have you overridden the Linker Flags on your target? You need to at least include $(inherited) if you have additional configuration on the target level. You should see -ObjC appear within your linker flags when viewing the setting in Xcode.