围绕 iPhone 开发的 JSON API 设计 DAO

发布于 2024-09-05 15:49:54 字数 688 浏览 6 评论 0原文

因此,我一直在尝试设计一种干净的方式来为 iPhone 上的模型获取数据。我的应用程序的所有数据都来自 JSON API。

因此,现在当 VC 需要一些模型时,它会自行执行 JSON 调用(异步),当它收到数据时,它会构建模型。它有效,但我正在尝试考虑一种更干净的方法,DAO 可以通过该方法为我检索信息并返回模型,所有这些都以异步方式进行。

我最初的想法是为我的 DAO 构建一个协议,以便 VC 实例化一个 DAO 并使自己成为委托。当您请求数据 [DAOinstance getAllUsers] 时,DAO 将执行所有网络请求操作,然后当它获得数据时,它将调用其委托(VC)上的方法来传递数据。

所以我认为这是一个很酷的解决方案,但意识到如果我需要在同一个 VC 中将相同的 DAO 用于不同的目的,我的委托方法将必须根据哪个 DAO 实例发起请求来分支逻辑。

所以我的第二个想法是能够按照典型的 javascript 模式将“处理程序”选择器传递给 DAO 对象。因此,我会说类似 [DAOinstance getAllUsersWithSelector:"TheHandlerFunctionOnMyVC:"] 的内容,而不是官方协议,然后当 DAO 完成其网络活动时,它将调用 VC 上传递的选择器,并将数据传回。

那么我在这里完全走错方向了吗?看起来也许是一个不错的选择。

任何关于设计这种数据层的指南或文章都会很不错。

谢谢! 鲍勃

So I've been trying to design a clean way of grabbing data for my models in iPhone land. All the data for my application is coming from JSON API's.

So right now when a VC needs some models, it does the JSON call itself (asynch) and when it receives the data, it builds the models. It works, but I'm trying to think of a cleaner method whereby the DAO's retrieve the information for me and return the models, all in an async manner.

My initial thought is build a protocol for my DAOs, such that the VC would instantiate a DAO and make itself the delegate. When you requested data [DAOinstance getAllUsers] the DAO would do all the network request stuff, and then when it had the data, it would call a method on its delegate (the VC) to pass the data.

So I think that's a cool solution, but realized that if I needed to use the same DAO for different purposes in the same VC, my delegate method would have to branch logic depending on which DAO instance initiated the request.

So my second thought was to be able to pass 'handler' selectors to the DAO object a la typical javascript patterns. So instead of an official protocol, I would say something like [DAOinstance getAllUsersWithSelector:"TheHandlerFunctionOnMyVC:"] Then when the DAO completed its network activities, it would call the passed selector on the VC, and pass the data back.

So am I headed in the wrong direction entirely here? Seems like maybe an ok way to go.

Any pointers or articles on designing this kind of data layer would be sweet.

Thanks!
Bob

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗地喜欢 2024-09-12 15:49:54

当您需要回调并且不想在处理程序方法调用者上分支时,传递目标(此处为您的 VC)和选择器(处理程序方法)是很常见的。这称为目标选择器模式。

除此之外,您可能还想查看 RestfulCoreDataCoreResource 框架介绍了可能的设计。

此外,著名的 ObjectiveResource 框架可能会提供很好的见解。

It's pretty common to pass the a target (your VC here) and a selector (the handler method) when you need a call back and don't want to branch on the handler method caller. This is called the target-selector pattern.

Apart from that you might want to check out the RestfulCoreData and CoreResource frameworks on possible designs of this.

Also the famous ObjectiveResource framework might provide a good insight.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文