2012-11-16 35 views
0

因此,我切換到使用RestKit 0.2和CoreData,並且我一直在嘗試使映射正確時遇到很多麻煩......我不知道不明白爲什麼。我的服務器的JSON響應是這樣的:「無法找到給定內容的任何映射,keyPath = null」RestKit 0.2

{ 
"meta": 
    { 
    "limit": 20, 
    "next": null, 
    "offset": 0, 
    "previous": null, 
    "total_count": 2 
    }, 
    "objects": 
    [{ 
     "creation_date": "2012-10-15T20:16:47", 
     "description": "", 
     "id": 1, 
     "last_modified": 
     "2012-10-15T20:16:47", 
     "order": 1, 
     "other_names": "", 
     "primary_name": "Mixing", 
     "production_line": "/api/rest/productionlines/1/", 
     "resource_uri": "/api/rest/cells/1/" 
    }, 
    { 
     "creation_date": "2012-10-15T20:16:47", 
     "description": "", 
     "id": 2, 
     "last_modified": "2012-10-15T20:16:47", 
     "order": 2, "other_names": "", 
     "primary_name": "Packaging", 
     "production_line": "/api/rest/productionlines/1/", 
     "resource_uri": "/api/rest/cells/2/" 
    }] 
} 

然後在XCode中我有:

RKObjectManager *objectManager = [RKObjectManager sharedManager]; 

[AFNetworkActivityIndicatorManager sharedManager].enabled = YES; 

NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil]; 
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel]; 
objectManager.managedObjectStore = managedObjectStore; 


RKEntityMapping *cellMapping = [RKEntityMapping mappingForEntityForName:@"Cell" inManagedObjectStore:managedObjectStore]; 
cellMapping.primaryKeyAttribute = @"identifier"; 
[cellMapping addAttributeMappingsFromDictionary:@{ 
@"id": @"identifier", 
@"primary_name": @"primaryName", 
}]; 



RKResponseDescriptor *responseCell = [RKResponseDescriptor responseDescriptorWithMapping:cellMapping 
                      pathPattern:@"/api/rest/cells/?format=json" 
                       keyPath:@"objects" 
                      statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 


[objectManager addResponseDescriptorsFromArray:@[responseCell, responseUser, responseCompany]]; 


[managedObjectStore createPersistentStoreCoordinator]; 
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"AppDB.sqlite"]; 
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"SeedDatabase" 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]; 

我的要求是:

[[RKObjectManager sharedManager] getObjectsAtPath:@"/api/rest/cells/?format=json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 
     RKLogInfo(@"Load complete: Table should refresh..."); 

     [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"LastUpdatedAt"]; 
     [[NSUserDefaults standardUserDefaults] synchronize]; 
    } failure:^(RKObjectRequestOperation *operation, NSError *error) { 
     RKLogError(@"Load failed with error: %@", error); 
    }]; 

,我總是得到以下錯誤:

**Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "Unable to find any mappings for the given content" UserInfo=0x1102d500 {DetailedErrors=(), NSLocalizedDescription=Unable to find any mappings for the given content, keyPath=null}** 

非常感謝!

UPDATE:我已經添加了cellMapping.forceCollectionMapping = YES; 但仍沒有運氣:(

更新#2:!隨着布雷克的意見,我試圖改變路徑和它的工作我做了/ API /休息/ /而不是細胞/ API/REST /細胞/格式= JSON和我的服務器返回我的一切,映射成功

現在,我得到的唯一的問題是以下錯誤:

2012年11月21日14:48:49.414應用[ 3125:617] W restkit.object_mapping:RKMapperOperation.m:176集合映射強制但可映射的對象類型爲'__NSCFArray'而是比NSDictionary

回答

4

聽起來好像響應描述符無法匹配請求的URL。兩個想法:

  1. 嘗試完全去除所述路徑圖案(在零傳遞),只是使用在「細胞」
  2. 嘗試使用「/ API /休息/細胞」的路徑圖案的關鍵路徑基於匹配

接下來我要嘗試的是使用調試器來逐步進行匹配。在RKObjectManager之內,候選響應映射列表由RKFilteredArrayOfResponseDescriptorsMatchingPath函數構建。如果您的預期響應映射沒有在那裏返回,那麼路徑模式的請求路徑無法評估。

如果事情看起來不錯,下一個可能會出現不匹配的地方是buildResponseMappingsDictionary方法中的RKResponseMapperOperation。該方法評估每個響應描述符的響應。如果回覆無法與您的回覆描述符相匹配,那麼您會在此處收到意想不到的結果。

最終要檢查的地方是在RKResponseMapperOperation中。這需要來自匹配描述符的映射並應用它們。 RKMapperOperation main方法應包含您預期的反序列化響應和mappingsDictionary屬性上的適當映射。

+0

不錯!我更新了我的問題,編輯:)! – abisson

+0

我遵循你的建議,然後刪除了關於強制映射的行,我已經和它的工作!再次感謝Blake! – abisson

0

即使設置似乎是正確的,映射是錯誤的。您映射調用的信息嵌套在給定對象內,因此您必須告訴映射以查找對象內的值。

RKEntityMapping *cellMapping = [RKEntityMapping mappingForEntityForName:@"Cell" inManagedObjectStore:managedObjectStore]; 
cellMapping.primaryKeyAttribute = @"identifier"; 

[cellMapping mapKeyOfNestedDictionaryToAttribute:@"objects"]; 
[cellMapping mapFromKeyPath:@".id" toAttribute:"identifier"]; 
[cellMapping mapFromKeyPath:@"primary_name" toAttribute:"primaryName"]; 

欲瞭解更多信息,請查閱RKObjectMapping Reference

+0

事情是,函數mapFromKeyPath只在RKObjectMapping中...而我正在使用CoreData。因此,我有一個RKEntityMapping,而mapFromKeyPath不在RKEntityMapping中。 – abisson

+0

我下週更新到RestKit 0.20,所以我會讓你知道我發現了關於配置那裏映射的過程。我的建議:如果項目緊急,則使用主分支(10.3),因爲0.20正在大量開發中。 – flashfabrixx