使用RKEntityMapping和RKRelationshipMapping获取许多记录

时间:2016-02-27 10:03:36

标签: ios swift core-data restkit

我找不到正确的映射并获得许多记录。

实体只有两个属性nid(Integer 16)和body(String)。

json(由Drupal Webservice创建,只是其中的一部分)就像

[{
"nid":[{"value":"4"}],
"body":[{"value":"<p>test test test<\/p>   \r\n","format":"basic_html","summary":""}]
}]

和我的代码

    let mapping = RKEntityMapping(forEntityForName: "Content", inManagedObjectStore: manager.managedObjectStore)
    mapping.identificationAttributes = ["nid"]

    let nidMapping = RKEntityMapping(forEntityForName: "Content", inManagedObjectStore: manager.managedObjectStore)
    nidMapping.addAttributeMappingsFromDictionary([
        "value"      : "nid"
        ])

    let bodyMapping = RKEntityMapping(forEntityForName: "Content", inManagedObjectStore: manager.managedObjectStore)
    bodyMapping.addAttributeMappingsFromDictionary([
        "value"      : "body"
        ])

    mapping.addPropertyMapping(RKRelationshipMapping(fromKeyPath: "nid", toKeyPath: "nid", withMapping: nidMapping))
    mapping.addPropertyMapping(RKRelationshipMapping(fromKeyPath: "body", toKeyPath: "body", withMapping: bodyMapping))


    let responseDescriptor = RKResponseDescriptor(
        mapping: mapping,
        method: RKRequestMethod.GET,
        pathPattern: "",
        keyPath: "",
        statusCodes: NSIndexSet(index: 200)
    )

我得到3个nsmanagedobjects而不是1:

nid nil, body nil
nid 0, body <p>test test test</p>
nid 4, body nil

谢谢!

1 个答案:

答案 0 :(得分:1)

和Wain一样,我不得不使用自定义值转换器而不是RKRelationshipMapping:

    let transformer = RKBlockValueTransformer(validationBlock: { (inputClass:AnyClass!,outputClass:AnyClass!) -> Bool in

        if inputClass.isSubclassOfClass(NSArray.self) { //__NSCFArray
            return true
        }

        return false

        }) { (inputValue:AnyObject!, outputValue, outputClass, error) -> Bool in

            if let arr = inputValue as? NSArray {
                if let dic = arr[0] as? NSDictionary {
                    if(outputClass.isSubclassOfClass(NSString.self)) {
                        if let str = dic["value"] as? NSString {
                            outputValue.memory = str //outputValue is AutoreleasingUnsafeMutablePointer<AnyObject?>
                            return true
                        }
                    }
                    if(outputClass.isSubclassOfClass(NSNumber.self)) {
                        if let nr = dic["value"] as? String {
                            outputValue.memory = Int(nr)
                            return true
                        }
                    }
                }
            }

            return false
    }

    RKValueTransformer.defaultValueTransformer().addValueTransformer(transformer)

    let mapping = RKEntityMapping(forEntityForName: "Content", inManagedObjectStore: manager.managedObjectStore)
    mapping.addAttributeMappingsFromDictionary([
        "nid"      : "nid",
        "body"      : "body"
        ])

    let responseDescriptor = RKResponseDescriptor(
        mapping: mapping,
        method: RKRequestMethod.GET,
        pathPattern: "",
        keyPath: "",
        statusCodes: NSIndexSet(index: 200)
    )