7

RestKitを使用してJSONを解析し、CoreDataNSManagedObjectsにマッピングしています。これがサンプルJSONです。

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

を使用RKObjectMappingProviderして、配列を個別のCoreDataオブジェクトRKManagedObjectMappingにマッピングしています。これは正常に機能します。"events"Event

Eventこれで、とに2つの関係がUserありますPhoto。次に、の値に基づいてサブジェクト配列を適切なCore Dataオブジェクトにマップし、"subject_type"それをの正しい関係に設定する必要がありますEvent

使ってみRKDynamicObjectMappingましたが、「動的な関係」にそれを指定する方法がわかりません。の値に基づいて宛先関係の名前を設定する方法が必要ですsubject_type

何かご意見は?

4

3 に答える 3

0

利用できるはずですRKDynamicObjectMappingオブジェクトマッピングを参照)。RKDynamicObjectMappingユーザーマッピングと写真マッピングを作成して、次のように使用できます。

[dynamicMapping setObjectMapping:userMapping 
              whenValueOfKeyPath:@"subject_type" 
                       isEqualTo:@"user"];
[dynamicMapping setObjectMapping:photoMapping 
              whenValueOfKeyPath:@"subject_type" 
                       isEqualTo:@"photo"];
于 2012-11-04T14:19:00.403 に答える
0

最近、この問題に遭遇しました。RestKit をトレースすると、すべてのオブジェクト マッピングを RKDynamicMapping インスタンスのすべての関係に適用しようとしているように見えます。applyRelationshipMappingsRKMappingOperation.mを参照してください。

ハックである解決策を思いつきましたが、レストキット コードを大幅に変更する必要はありませんでした。

RKMappingOperation.m で、次のメソッドを変更しました。

destinationObjectForMappingRepresentation:parentRepresentation:withMapping:inRelationship:

次のコード (コメントで始まる) を追加しました。このコードは、リレーションシップの宛先が適用されているのと同じタイプのオブジェクトであるかどうかを確認し、一致する場合にのみ続行します。(これは厳密なテストを受けていませんが、私の特定のユースケースでは機能しています。)

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

...

于 2015-08-20T05:34:07.893 に答える