2

我通过 CocoaPods 安装了 RestKit (0.20.0-pre3) 并按照文档说明对其进行了配置。执行 getObjectsAtPath 和 RestKit 时,我得到了映射:

*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSManagedObjectContext saveToPersistentStore:]:无法识别的选择器发送到实例 0x9eaae40”

如果我设置断点,它会在第 278 行的 RKManagedObjectRequestOperation.m 处停止。

我将 RestKit 配置如下:

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


}

谢谢你的帮助!

4

1 回答 1

0

这很可能表明您的链接器标志设置不正确。需要链接器-ObjC标志才能加载类别方法。您是否覆盖了目标上的链接器标志?$(inherited)如果您在目标级别上有其他配置,您至少需要包括。在 Xcode 中查看设置时,您应该会看到-ObjC出现在链接器标志中。

于 2012-12-15T16:39:43.673 回答