RestKit,RKEntityMapping 响应描述符的问题

RestKit, RKEntityMapping trouble with Response Descriptors

我正在尝试将 RestKit 与 RKEntityMapping 一起使用;( 求助!

  NSURL *baseURL = [NSURL URLWithString:@"http://api.mymemory.translated.net"];

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];


// Initialize managed object model from bundle
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
// Initialize managed object store
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;


// Complete Core Data stack initialization
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"MyArticlesDB.sqlite"];
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"RKSeedDatabase" ofType:@"sqlite"];
NSError *error;

NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);

// Create the managed object contexts
[managedObjectStore createManagedObjectContexts];

// Configure a managed object cache to ensure we do not create duplicate objects
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];

RKEntityMapping *matchMapping = [RKEntityMapping mappingForEntityForName:@"Match" inManagedObjectStore:managedObjectStore];
[matchMapping addAttributeMappingsFromDictionary:@{@"id"    :@"id",
                                                   @"segment"   :@"segment",
                                                   @"trnaslation"   :@"translation",
                                                   @"quality"       :@"quality",
                                                   @"reference"     :@"reference",
                                                   @"usage-count"   :@"usage_count",
                                                   @"subject"       :@"subject",
                                                   @"created-by"    :@"MateCat",
                                                   @"last-updated-by"   :@"last_updated_by",
                                                   @"create-date"       :@"create_date",
                                                   @"last-update-date"  :@"last_update_date",
                                                   @"tm_properties"     :@"tm_properties",
                                                   @"match"         :@"match"}];


RKEntityMapping *translationMapping = [RKEntityMapping mappingForEntityForName:@"Translation" inManagedObjectStore:managedObjectStore];
[translationMapping addAttributeMappingsFromDictionary:@{@"responseData.translatedText"     :@"translatedText",
                                                         @"responseData.match"              :@"match",
                                                         @"responseDetails"                 :@"responseDetails",
                                                         @"responseStatus"                  :@"responseStatus",
                                                         @"responseId"                      :@"responseId"}];









      RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:translationMapping method:RKRequestMethodGET pathPattern:@"/" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];

json 这里:http://api.mymemory.translated.net/get?q=Hello%20World!&langpair=en%7Cru

错误:

Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the response loaded." UserInfo={NSLocalizedFailureReason=A 200 response was loaded from the URL 'http://api.mymemory.translated.net/get?q=Hello%20World!&langpair=en%7Cru', which failed to match all (0) response descriptors:, NSErrorFailingURLStringKey=http://api.mymemory.translated.net/get?q=Hello%20World!&langpair=en%7Cru, NSErrorFailingURLKey=http://api.mymemory.translated.net/get?q=Hello%20World!&langpair=en%7Cru, NSUnderlyingError=0x1700562f0 {Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched." UserInfo={NSLocalizedDescription=No mappable object representations were found at the key paths searched., NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: The representation inputted to the mapper was found to contain nested object representations at the following key paths: matches, responderId, responseData, responseDetails, responseStatus This likely indicates that you have misconfigured the key paths for your mappings., keyPath=null, DetailedErrors=( )}}, keyPath=null, NSLocalizedDescription=No response descriptors match the response loaded.}

我忘记添加关系了。这对我有帮助。

RKRelationshipMapping *res = [RKRelationshipMapping relationshipMappingFromKeyPath:@"responseData" toKeyPath:@"responseData" withMapping:responseMapping];

 RKRelationshipMapping *matchRelation = [RKRelationshipMapping relationshipMappingFromKeyPath:@"matches" toKeyPath:@"matches" withMapping:matchMapping];


[globalMappig addPropertyMapping:matchRelation];
[globalMappig addPropertyMapping:responseRelation];