RestKit - Objectmapping!这里出了什么问题?

时间:2011-10-19 17:04:46

标签: objective-c core-data null restkit object-object-mapping

我的服务器有JSON响应。 从RestKit日志中获取的数据如下所示:

sourceObject: ( 
    { 
    place =         { 
        "place_id" = "3cc1e372-f9d9-11e0-aba9-9415ab1a8034"; 
        placeinfo =             { 
            latitude = "12.5846738815"; 
            longtitude = "55.6815948486"; 
            "place_id" = "3cc1e372-f9d9-11e0-aba9-9415ab1a8034"; 
        }; 
        timestamp = "2011-10-19 00:33:44"; 
    }; 
}, 
    { 
    place =         { 
        "place_id" = "b65e36e0-f9d9-11e0-aba9-9415ab1a8034"; 
        placeinfo =             { 
            latitude = "12.5720376968"; 
            longtitude = "55.6785774231"; 
            "place_id" = "b65e36e0-f9d9-11e0-aba9-9415ab1a8034"; 
        }; 
        timestamp = "2011-10-19 00:37:08"; 
    }; 
}, 
    { 
    Timestamps =         { 
        "latest_update" = "2011-10-18 17:12:09"; 
        uuid = "8c6fb842-f99b-11e0-aba9-9415ab1a8034"; 
    }; 
} )and targetObject: (null) 

数据被映射到3个对象:

Place,Placeinfo和LatestDBUpdate。

问题是:

在上面的JSON响应中,有2个位置对象和2个嵌套的PlaceInfo对象,以一对多关系连接。

还有一个与Timestamps关键字相关的LatestDBUpdate对象。

Restkit将映射3个Place对象,其中一个将为NULL。 其他2个Place对象正确映射,关系也正确。 它当然应该只映射2个放置对象!

LatestDBUpdate映射还将时间戳映射正确。

我已经确定,如果我删除了JSON响应的Timestamps部分,那么映射是正确的,只有2个位置对象。

但我需要Timestamps部分在那里! 我不知道如何解决这个问题!任何想法 - 我真的可以使用一些输入,因为我已经用了几个小时了!

以下是使用的映射设置:

//Place mapping 
if (!self.placeManagedObject){ 
    self.placeManagedObject = [RKManagedObjectMapping mappingForClass:[Place class]]; 
    self.placeManagedObject.primaryKeyAttribute = @"UUID"; 
    self.placeManagedObject.setDefaultValueForMissingAttributes = YES; 
    [self.placeManagedObject mapKeyPath:@"place_id" toAttribute:@"UUID"]; 
    [self.placeManagedObject mapKeyPath:@"timestamp" toAttribute:@"timestamp"]; 
} 
//PlaceInformation mapping 
if (!self.placeInfoManagedObject){ 
    self.placeInfoManagedObject = [RKManagedObjectMapping mappingForClass:[PlaceInfo class]]; 
    self.placeInfoManagedObject.primaryKeyAttribute = @"UUID";self.placeInfoManagedObject.setDefaultValueForMissingAttributes = YES; 
    [placeInfoManagedObject mapKeyPath:@"place_id" toAttribute:@"UUID"]; 
    [placeInfoManagedObject mapKeyPath:@"longtitude" toAttribute:@"longtitude"]; 
    [placeInfoManagedObject mapKeyPath:@"latitude" toAttribute:@"latitude"]; 
} 
//latestDBUpdate timestamp mapping 
if (!self.latestDBUpdateManagedObject){ 
    self.latestDBUpdateManagedObject = [RKManagedObjectMapping mappingForClass:[LatestDBUpdate class]]; 
    self.latestDBUpdateManagedObject.primaryKeyAttribute = @"latest_update"; 
    [self.latestDBUpdateManagedObject mapKeyPath:@"latest_update"toAttribute:@"latest_update"]; 
    [self.latestDBUpdateManagedObject mapKeyPath:@"uuid" toAttribute:@"uuid"]; 
} 
//Set mapping relations 
[self.placeManagedObject mapRelationship:@"placeinfo" withMapping:self.placeInfoManagedObject]; 
// Register mapping with the provider - 
[self.objectManager.mappingProvider setMapping:self.placeManagedObject forKeyPath:@"place"]; 
[self.objectManager.mappingProvider setMapping:self.placeInfoManagedObject forKeyPath:@"placeinfo"]; 
[self.objectManager.mappingProvider setMapping:self.latestDBUpdateManagedObject forKeyPath:@"Timestamps"]; 

2 个答案:

答案 0 :(得分:1)

再回来,回答我自己的问题......似乎这里使用Restkit的成员不多!

我的解决方案,也许并不漂亮,但唯一有效的,没有 删除JSON响应的Timestamps部分:

我通过以下方式修改了JSON响应:

{ 
placeList =     ( 
            { 
        place =             { 
            "place_id" = "3cc1e372-f9d9-11e0-aba9-9415ab1a8034"; 
            placeinfo =                 { 
                latitude = "12.5846738815"; 
                longtitude = "55.6815948486"; 
                "place_id" = "3cc1e372-f9d9-11e0-aba9-9415ab1a8034"; 
            }; 
            timestamp = "2011-10-19 00:33:44"; 
        }; 
    }, 
            { 
        place =             { 
            "place_id" = "b65e36e0-f9d9-11e0-aba9-9415ab1a8034"; 
            placeinfo =                 { 
                latitude = "12.5720376968"; 
                longtitude = "55.6785774231"; 
                "place_id" = "b65e36e0-f9d9-11e0-aba9-9415ab1a8034"; 
            }; 
            timestamp = "2011-10-19 00:37:08"; 
        }; 
    } 
); 
updateData =     { 
    timestamps =         { 
        "latest_update" = "2011-10-18 17:12:09"; 
        uuid = "8c6fb842-f99b-11e0-aba9-9415ab1a8034"; 
    }; 
};} 

为地点添加一种主键 - > placeList和updateData用于时间戳。

我还需要更改提供程序的密钥路径以反映对JSON输入的更改:

// Register mapping with the provider - 
[self.objectManager.mappingProvider setMapping:self.placeManagedObject forKeyPath:@"placeList.place"]; 
[self.objectManager.mappingProvider setMapping:self.placeInfoManagedObject forKeyPath:@"placeinfo"]; 
[self.objectManager.mappingProvider setMapping:self.latestDBUpdateManagedObject forKeyPath:@"updateData.timestamps"]; 

所以它解决了它。没有更多的NULL对象,映射,即使你没有 非常明白为什么对JSON的改变如此之大 映射的差异。!

答案 1 :(得分:0)

首先,如果您在提出这样的问题时发布有效的JSON会更有帮助 - 您可能会收到更多回复。

其次,第一次尝试的主要问题是将Timestamps对象分组为sourceObject数组的一部分。你可能会用以下内容得到你想要的东西:

{
    "sourceObject": [
        {
            "place": {
                "place_id": "3cc1e372-f9d9-11e0-aba9-9415ab1a8034", 
                "placeinfo": {
                    "latitude": "12.5846738815", 
                    "longtitude": "55.6815948486", 
                    "place_id": "3cc1e372-f9d9-11e0-aba9-9415ab1a8034"
                }, 
                "timestamp": "2011-10-19 00:33:44"
            }
        }, 
        {
            "place": {
                "place_id": "b65e36e0-f9d9-11e0-aba9-9415ab1a8034", 
                "placeinfo": {
                    "latitude": "12.5720376968", 
                    "longtitude": "55.6785774231", 
                    "place_id": "b65e36e0-f9d9-11e0-aba9-9415ab1a8034"
                }, 
                "timestamp": "2011-10-19 00:37:08"
            }
        }
    ],
    "Timestamps": {
        "latest_update": "2011-10-18 17:12:09", 
        "uuid": "8c6fb842-f99b-11e0-aba9-9415ab1a8034"
    }
}