问题:
在我的应用程序中,用户需要先通过登录屏幕。一旦用户成功登录,我就会存储对该用户核心数据对象的引用。当这个用户注销而另一个用户登录时,我的问题就出现了。我目前无法在同一设备上区分用户 A 数据和用户 B 数据。
对服务器的调用在 header 中传递用户的凭据,但返回没有关于请求用户在映射期间使用的外键信息。我想将其设置为简单地使与用户的关系使用存储的引用。
这些关系往往是多对一(用户 <--->> 电子邮件)或多对多(用户 <<-->> session )。
...
{
heading: "My Email"
body: "I didn't write much",
...
},
...
可能的解决方案:
- (最需要的解决方案) 在映射期间执行连接
- 以某种方式设置 RKEntityMapping 以自动建立传入对象和引用用户之间的关系连接
- 请求服务器返回userID
- 这将允许使用用户 ID 键进行关系映射
- 映射后手动设置关系
- 使用 getObjectsAtPath:parameters:success:failure: 中的成功 block 连接用户和新收到的对象
非常感谢任何和所有反馈。谢谢。
最佳答案
(Most Desired Solution) Perform connection during mapping. Somehow set the
RKEntityMapping
to automatically make the relationship connection between the incoming objects and the referenced user
根据您的描述,您实际上没有任何可以触发此操作的内容。 如果您用于发出请求的 URL 中包含用户身份,那么您可以使用它并路由 metadata ,否则你就不走运了。 (元数据的文档不是很好,请参阅 this answer)
Request the server to return the
userID
. This would then allow the relationship mapping to occur using theuserID
key
是的,那行得通。
Manually set the relationship after mapping. Use the
success
block fromgetObjectsAtPath:parameters:success:failure:
to connect the user and newly received object(s)
是的,这也有效(虽然它会导致 2 次保存到数据存储,1 次没有关系,然后第二次建立关系)。
关于ios - 没有键映射的 RestKit 关系连接到特定的核心数据对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23836337/