我们有一个iOS应用程序可以从API中获取模型数据。 iOS设备的用户执行链接模型数据并在它们之间创建关系的动作。 iOS然后提交回API以保存关系,并允许在Web应用程序中查看关系。
如果与新关系关联的所有对象都使用其属性提交,则JSON有效内容可能会很大。
"peoples_vehicles": [
{
"owner" : {
"id" : 8282, <----------this would be a UUID
"name" : "John Smith",
"address": "123 Fake Street",
},
"vehicles" : [
{
"id" : 1234, <----------this would be a UUID
"name" : "FORD F150",
"make" : "F150",
"model" : "FORD",
"year" : "2017",
},
{
"id" : 5678, <----------this would be a UUID
"name" : "FORD ESCAPE",
"make" : "ESCAPE",
"model" : "FORD",
"year" : "2013",
}
]
},
{
... another person with their vehicles
}
]
由于发回的数据是最初来自API的所有数据,iOS应用程序是否应该打扰发回所有属性? iOS应用程序是否应该只返回与对象ID的所有关系?我们使用UUID
"peoples_vehicles": [
{
"owner" : {
"id" : 8282, <----------this would be a UUID
},
"vehicles" : [
{
"id" : 1234, <----------this would be a UUID
},
{
"id" : 5678, <----------this would be a UUID
}
]
},
{
... another person with their vehicles
}
]
我们似乎更倾向于为预先存在的数据发送ID。它使从iOS到API的提交有效负载具有非常少的实际字符串属性(在此示例中没有)。
有些情况下我们必须创建新的自定义对象。在这种情况下,将发送所有属性,并且此新对象的ID将为null,以指示必须创建此对象。
"peoples_vehicles": [
{
"owner" : {
"id" : 8282, <----this would be a pre-existing object
},
"vehicles" : [
{
"id" : 1234, <----this would be a pre-existing object
},
{
"id": null <----new object that needs to be saved
"name" : "FORD F150",
"make" : "F150",
"model" : "FORD",
"year" : "2017",
}
]
},
{
... another person with their vehicles
}
]
这会是一个不错的方法吗?以Stripe和Shopify API为例,这似乎很好用,但我想确保我没有遗漏任何东西,如果我应该包含预先存在的对象的属性。