如何手动设置NSManagedObjectID?
我想将 iPhone 上的核心数据与 Sinatra 服务器上的 MongoDB 同步。
当 iPhone 从 Node.js 聊天服务器获取消息时,该消息包含发送者的 BSON ObjectId(字符串)。为了让 iPhone 存储此消息,我在 Core Data 中找到具有相同 ID 的用户。我希望能够做类似 objectWithID
的事情。换句话说,我想将 NSManagedObjectID 设置为与 MongoDB BSON ObjectId 相同。我要检查的另一件事是,如果 ManagedObjectContext 中没有具有发送者 id 的用户,我将从 persistenceStore 中获取他,如果他不存在,我将创建他。只是为了维护 Message 和 Sender 之间的关系,要做的事情就很多。也许我应该将 Message 对象的发送者属性存储为其 BSON ObjectId 作为字符串,而不是作为 User NSManagedObject。
你有什么建议吗?我也应该在 iPhone 上使用 MongoDB 吗?我刚刚意识到有一个 Objective-C 驱动程序。但我刚刚开始掌握 Core Data 的窍门,它有我可能想要使用的很酷的东西(比如撤消支持)......现在,我只是用它来进行简单的获取和处理。持久存储,所以我可以替换它。但我计划在未来实现撤消支持,只是因为我认为这是很好的可用性。我更喜欢撤消选项,而不是必须回答确认弹出窗口,例如,我喜欢 GMail 如何让您撤消发送或撤消删除邮件等。
谢谢!
马特
I want to sync the Core Data on iPhone with MongoDB on Sinatra server.
When the iPhone gets a message from the Node.js chat server, the message contains the sender's BSON ObjectId (a string). For the iPhone to store this message, I find the user with that same Id in Core Data. I want to be able to do something like objectWithID
. In other words, I want to set the NSManagedObjectIDs to be the same as the MongoDB BSON ObjectIds. The other thing I would check is if there are no users with the sender's id in the managedObjectContext, I will fetch him from the persistentStore, and if he's not there, I'll create him. This is a lot to do just to maintain the relationship between Message and Sender. Maybe I should just store the sender attribute of the Message object as his BSON ObjectId as a string instead of as a User NSManagedObject.
What do you recommend? Should I just use MongoDB on the iPhone too? I just realized there's an Objective-C driver for it. But I was just starting to get the hang of Core Data, and it has cool things (like undo support) that I might want to use... Right now, I'm just using it for simple fetching & persistent storage, so I could replace it. But I plan to implement undo support in the future, just because I think that's good usability. I prefer the option to undo than having to answer a confirmation pop-up, e.g., I like how GMail lets you undo a send or undo a deletion of messages, etc.
Thanks!
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法手动设置核心数据对象的objectID。您可能需要添加一个名为 externalID 的属性或用于存储从服务器获取的 ID 的属性。
有关是否使用 Core Data 的一些想法,请参阅本文。请注意,作者的建议是在几乎所有情况下都坚持使用核心数据。
You can't set the objectID of a core data object manually. You'll probably want to add an attribute called externalID or something where you store the ID you got from the server.
See this article for some thoughts on whether or not to use Core Data. Note that the author's recommendation is to stick with Core Data in almost all cases.