简体   繁体   English

当涉及到数据和UI时,如何在类之间进行通信?

[英]How to communicate between classes when both data and UI are involved?

I'm working on a largely navigation-based iPhone app that communicates with a REST API using OAuth, and I'd like to know how my classes should best communicate with each other. 我正在开发一个基于导航的iPhone应用程序,该应用程序使用OAuth与REST API进行通信,并且我想知道我的类应该如何最好地相互通信。 I've got multiple UITableViews, multiple UITableViewDataSources, an API class, and a view controller that handles authentication in a web view. 我有多个UITableViews,多个UITableViewDataSources,一个API类和一个在Web视图中处理身份验证的视图控制器。

Here's how I have things structured now: 这是我现在结构化的方式:

  • My UIApplicationDelegate owns an instance of the class that knows how to communicate with the REST API 我的UIApplicationDelegate拥有该类的实例,该实例知道如何与REST API通信
  • I can ask the API class to call a REST method, and it returns with the data (it wraps ASIHTTPRequest s to handle OAuth transparently, so it doesn't currently know which object asked for the data) 我可以要求API类调用REST方法,并返回数据(它包装ASIHTTPRequest来透明地处理OAuth,因此它当前不知道哪个对象要数据)
  • If the user isn't authenticated, I can ask the API class to initiate the OAuth process 如果用户未通过身份验证,我可以要求API类启动OAuth流程
  • The OAuth dialog can be presented, and the application granted access to the API on the user's behalf 可以显示OAuth对话框,并且应用程序代表用户授权访问API

The trouble I'm running into is that my UITableViewDataSources need to communicate with the API class to fetch their data, but authentication might need to happen first, which involves a modal authentication view presented by a view controller. 我遇到的麻烦是,我的UITableViewDataSources需要与API类进行通信以获取其数据,但是身份验证可能需要首先进行,这涉及视图控制器提供的模式身份验证视图。

  1. Would it be better to have each UITableViewDataSource model own its own instance of the API class, or should the API class be a singleton? 最好让每个UITableViewDataSource模型拥有自己的API类实例,或者API类应该是单例?

  2. If each model owns an instance of the API class, how should it communicate to the view controller that authentication needs to happen? 如果每个模型都拥有API类的实例,则应如何与视图控制器沟通需要进行身份验证?

  3. If the API class is a singleton, how should it interact with multiple models and view controllers to present the authentication dialog? 如果API类是单例,那么它应如何与多个模型和视图控制器交互以显示身份验证对话框? Delegates for each? 每个代表? Post NSNotifications? 发布NSNotifications?

  4. Some other way I haven't thought of? 我没想到的其他方式?

Really, I think the core of the problem is that I have one class that's primarily used for data fetching purposes, but it might need user interaction to do so successfully. 确实,我认为问题的核心是我有一个主要用于数据获取目的的类,但是它可能需要用户交互才能成功完成。

I typically use a singleton exactly in the way you describe and it works well. 我通常按​​照您描述的方式使用单例,并且效果很好。 Here is how I would answer your questions. 这是我将如何回答您的问题。

  1. Singleton 辛格尔顿

  2. N/A. 不适用 Use a singleton. 使用单例。

  3. Notifications work well, but I tend to prefer to pass a delegate with each request and then keep a hold of it inside the singleton until the request has finished at which point I just call back to the delegate with a success or failure message. 通知运行良好,但是我倾向于将每个请求都传递给委托,然后将其保留在单例中,直到请求完成为止,此时,我仅使用成功或失败消息回叫给委托。 Notifications can get pretty messy if you have multiple view controllers that are live in your navigation stack all potentially listening and responding depending. 如果您有多个视图控制器是在你的导航堆栈的所有潜在的聆听和响应不同的通知可以得到相当混乱。 I've seen that introduce some really weird bugs. 我已经看到了一些非常奇怪的错误。 If you're careful, notifications work just fine, but passing a delegate allows you to associate a specific delegate, typically the current view controller, with a specific request which is often ideal. 如果您小心一点,通知就可以正常工作,但是传递委托可以使您将特定的委托(通常是当前的视图控制器)与特定的请求相关联,这通常是理想的。

  4. I think you're on the right track. 我认为您在正确的轨道上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM