【问题标题】:Obtaining to many records with RKEntityMapping and RKRelationshipMapping使用 RKEntityMapping 和 RKRelationshipMapping 获取多条记录
【发布时间】:2016-06-10 15:38:30
【问题描述】:

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

实体只有两个属性 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

谢谢!

【问题讨论】:

  • 显示实体。 JSON 很讨厌。
  • json 来自 drupal。我在帖子中添加了实体的属性类型。谢谢
  • 所以你有没有关系的普通值,但你正在使用关系映射?您需要自定义值转换器,而不是关系映射。

标签: ios swift core-data restkit


【解决方案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)
    )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多