简体   繁体   English

Objective-C - 从控制器将模型对象传递给工作者类

[英]Objective-C - Passing Model Objects to a Worker Class From a Controller

I am a complete newbie working on my first iPhone project. 我是第一个完成iPhone项目的新手。 I have been a .NET developer for 4 years to give you some background on where I am coming from. 我已经成为一名.NET开发人员已有4年时间,可以为您提供有关我来自哪里的背景知识。 I am trying to follow the MVC pattern and do things the right way on my first project. 我试图遵循MVC模式,并在我的第一个项目上以正确的方式做事。 I don't want to hack things together just to get them to work. 我不想为了让他们工作而一起破解。

Here is my situation: I am trying to parse an XML feed which will only contain 1 object. 这是我的情况:我正在尝试解析一个只包含1个对象的XML提要。 I have a model object which represents the object I will get from parsing the XML feed. 我有一个模型对象,它表示我将从解析XML feed获得的对象。 I have subclassed NSXMLParser and am able to successfully parse the XML feed and get values back (using NSLog to check the values). 我已经子类化了NSXMLParser,并且能够成功解析XML feed并获取值(使用NSLog来检查值)。 Here is where my disconnect occurs. 这是我断开连接的地方。 When moving from my controller to the subclass, what is the best way to call the XMLParser, populate the model object, and return it to the controller? 从我的控制器移动到子类时,调用XMLParser,填充模型对象并将其返回到控制器的最佳方法是什么?

I am looking for some kind of pattern to follow which would be considered a best practice. 我正在寻找一种可以被认为是最佳实践的模式。 I don't want to just throw all the logic into a method on the controller, making it unable to be reused in any situation. 我不想将所有逻辑都抛到控制器上的方法中,使其无法在任何情况下重用。

If you make your view controller implement the NSXMLParserDelegate protocol, and set the NSXMLParser instance's delegate property to your view controller, your view controller will know when the parser parses stuff and finishes its work . 如果使视图控制器实现NSXMLParserDelegate协议,并将NSXMLParser实例的委托属性设置为视图控制器,则视图控制器将知道解析器何时解析内容完成其工作 In other words, the view controller can consume a populated data model, once the parser subclass tells the delegate it is done. 换句话说,一旦解析器子类告诉委托它完成,视图控制器就可以使用填充的数据模型。

As an aside, delegation is one of a few design patterns that OS X applications (and, specifically, Apple APIs) make heavy use of. 另外, 委托是OS X应用程序(特别是Apple API)大量使用的一些设计模式之一。 As you do iPhone development, you'll likely find and make use of delegates everywhere. 在进行iPhone开发时,您可能会在任何地方找到并使用代理。

If you don't want to use delegates, another option is to implement an observer pattern, issuing notifications from the NSNotificationCenter to observers, which call their selectors (methods) when they hear a notification. 如果您不想使用委托,则另一个选项是实现观察者模式,从NSNotificationCenter向观察者发出通知 ,观察者在听到通知时调用其选择器(方法)。 The advantage of notifications over delegates is that you can have lots of objects listen for notifications, whereas only one object can generally be a delegate at a given time. 通知超过委托的优点是您可以让许多对象侦听通知,而在给定时间只有一个对象通常可以是委托。

you can create a singleton class, that will manage all of your shared actions. 您可以创建一个单例类,它将管理您的所有共享操作。 You can find a quick tutorial of creating singleton objects here 您可以在此处找到创建单例对象的快速教程

I would put the parsing operation in an NSOperation, inside an operation queue. 我会将解析操作放在操作队列中的NSOperation中。 Then it can get performed on a background thread (note that you have to do some extra work to make that happen for asynch operations in an NSOperation). 然后它可以在后台线程上执行(请注意,您必须做一些额外的工作才能在NSOperation中进行异步操作)。

When the parsing is all done, store the results somewhere other controllers can see, and issue a notification that the object is ready. 解析完成后,将结果存储在其他控制器可以看到的位置,并发出对象已准备好的通知。 You may also want to issue a notification for error conditions, so that controllers waiting for a load to complete know they'll never get an object. 您可能还希望发出错误条件通知,以便等待加载完成的控制器知道它们永远不会获得对象。

You could also use a delegate as noted by Alex, but notifications are generally more flexible as then you can have a few different objects react to the load. 您也可以使用Alex指出的委托,但通知通常更灵活,因为您可以让一些不同的对象对负载做出反应。

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

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