2012-10-31 11 views
7

RestKitを使用してJSONを解析し、コアデータNSManagedObjectsにマップしています。ここにJSONのサンプルを示します。 RKObjectMappingProviderRKManagedObjectMappingを使用してRestKit値に基づいて関係名を動的にマップする

{ 
    "events": [ 
     { 
      "description": "...", 
      "subject_type": "photo", 
      "subject": { 
       "id": 1, 
       "thumb_url": "...", 
       "medium_url": "...", 
       "large_url": "..." 
      } 
     }, 
     { 
      "description": "...", 
      "subject_type": "user", 
      "subject": { 
       "id": 1, 
       "username": "...", 
       "followers": "..." 
      } 
     } 
    ] 
} 

私は別のコアデータEventオブジェクトに"events"配列をマッピングしています。これは正常に動作します。

Eventには、UserPhotoという2つの関係があります。今私は"subject_type"の値に基づいて適切なコアデータオブジェクトに主題配列をマップし、Eventの正しい関係にそれを設定する必要があります。

私はRKDynamicObjectMappingを使用しようとしましたが、「動的関係」の指定方法はわかりません。 subject_typeの値に基づいて、宛先関係の名前を設定する方法が必要です。

どのような考えですか?

+0

を、私は誰かがここで実際の質問に答えたいです。 – magma

答えて

1

また、指定したブロックの使用は、あなたが記述のような、より複雑な動的マッピング操作を行うことができます。この能力を与える具体的な方法は、ここでsetObjectMappingForRepresentationBlock

と呼ばれている方法のいくつかのドキュメントです: http://restkit.org/api/latest/Classes/RKDynamicMapping.html#//api/name/setObjectMappingForRepresentationBlock

RestKitユニットテストは、この方法のためのいくつかの簡単な例を示しています。

0

RKDynamicObjectMappingObject Mappingを参照)を利用できるはずです。あなたはユーザーマッピングとフォトマッピングを作成することができ、その後、あなたのRKDynamicObjectMappingは次のように使用します。RestKit、RKDynamicMapping

[dynamicMapping setObjectMapping:userMapping 
       whenValueOfKeyPath:@"subject_type" 
         isEqualTo:@"user"]; 
[dynamicMapping setObjectMapping:photoMapping 
       whenValueOfKeyPath:@"subject_type" 
         isEqualTo:@"photo"]; 
+0

これは適切な関係を使用するように、これを 'Event'エンティティでどのように設定するのですか? – brynbodayle

0

最近この問題が発生しました。 RestKitをトレースすると、RKDynamicMappingインスタンスのすべてのリレーションシップにすべてのオブジェクトマッピングを適用しようとしているようです。

私はハックの解決策を考え出しましたが、Restkitコードを大きく変更する必要はありませんでした。

destinationObjectForMappingRepresentation:parentRepresentation:withMapping:inRelationship: 

私は(コメントで始まる)以下のコードを追加されているような関係の宛先オブジェクトの同じタイプであるかどうかをチェックする。RKMappingOperation.mで

は、私は、次の方法を改変しました適用され、一致する場合にのみ進行します。 (これは、厳密なテストを受けていないが、私の特定のユースケースで働いている。)

- (id)destinationObjectForMappingRepresentation:(id)representation parentRepresentation:(id)parentRepresentation withMapping:(RKMapping *)mapping inRelationship:(RKRelationshipMapping *)relationshipMapping 
{ 
    RKObjectMapping *concreteMapping = nil; 
    if ([mapping isKindOfClass:[RKDynamicMapping class]]) { 
     concreteMapping = [(RKDynamicMapping *)mapping objectMappingForRepresentation:representation]; 
     if (! concreteMapping) { 
      RKLogDebug(@"Unable to determine concrete object mapping from dynamic mapping %@ with which to map object representation: %@", mapping, representation); 
      return nil; 
     } 
     // Make sure the destination of a core data relationship is the right entity class for the object we're mapping; 
     if ([self.destinationObject respondsToSelector:@selector(managedObjectContext)]) 
     { 
      NSEntityDescription *destinationEntity = [self.destinationObject entity]; 
      NSDictionary *destinationPropertiesDictionary = [destinationEntity propertiesByName]; 
      NSRelationshipDescription *destinationPropertyForDestinationKeyPath = [destinationPropertiesDictionary valueForKey:relationshipMapping.destinationKeyPath]; 
      NSString *relationshipDestinationClassName = [[destinationPropertyForDestinationKeyPath destinationEntity] name]; 
      NSString *mappedObjectClassName = [NSString stringWithCString:class_getName(concreteMapping.objectClass) encoding:NSUTF8StringEncoding]; 
      if (![relationshipDestinationClassName isEqualToString:mappedObjectClassName]) 
      { 
       return nil; 
      } 
     } 

...

関連する問題