简体   繁体   English

Core Data iPhone App的设计模式

[英]Design pattern for Core Data iPhone App

Im building an app that will use a Core Data model. 我正在构建一个将使用核心数据模型的应用程序。 I pretty new at Objective C and my usual design patterns does not really apply on Core Data and Objective C, at least I can't seem to find examples that confirms they will. 我在Objective C上很新,我的常用设计模式并不适用于Core Data和Objective C,至少我似乎无法找到确认它们的例子。

I have been through the Apple Developer examples and different sources on the intertubes. 我已经浏览了Apple Developer示例和intertubes上的不同来源。

It seems that to leverage Core Data I need to pass the managedObjectContext to each of my viewControllers, have the viewController implement the NSFetchedResultsControllerDelegate and then implement each of the methods for doing a fetch and subsequently implement 似乎要利用Core Data我需要将managedObjectContext传递给我的每个viewControllers,让viewController实现NSFetchedResultsControllerDelegate,然后实现每个方法进行获取并随后实现

NSFetchedResultsChangeInsert

NSFetchedResultsChangeDelete NSFetchedResultsChangeMove NSFetchedResultsChangeUpdate NSFetchedResultsChangeDelete NSFetchedResultsChangeMove NSFetchedResultsChangeUpdate

This adds approximately 100+ lines of code in each viewController and it is 90% the same code I write again and again. 这在每个viewController中增加了大约100行代码,并且它是我反复编写的相同代码的90%。 Plus I have to pass everything around and keep track of it's memory footprint. 另外,我必须传递一切并跟踪它的内存占用。

In other languages I would build a singleton model of a few classes that held methods for maintaining and delivering data upon request, available from anywhere. 在其他语言中,我将构建一个包含几个类的单例模型,这些类包含根据请求维护和提供数据的方法,可从任何地方获得。 It seems I can't take that approach in Objective C. If I where to build a static Class that took a managedObjectContext and returned me what I needed, I would still have to pass the managedObjectContext around to every view and it wouldn't be asynchronously like when I implement delegate methods that just gets called when a result is ready. 我似乎无法在Objective C中采用这种方法。如果我在哪里构建一个静态类,它接受了一个managedObjectContext并返回了我需要的东西,我仍然需要将managedObjectContext传递给每个视图,它不会是异步,就像我实现在结果准备就绪时调用的委托方法一样。

I hope this makes sense and that someone can either confirm that there is no other reasonable way to do it or help point me in a direction for wrapping this up in a good way. 我希望这是有道理的,并且有人可以确认没有其他合理的方法来做到这一点,或者帮助指出我的方向以良好的方式包装它。

Thanks:) 谢谢:)

Core Data is not nearly as complicated as you describe. 核心数据并不像您描述的那么复杂。

Generally, an iPhone app has a "main" managed object context, which is generally owned by the app delegate. 通常,iPhone应用程序具有“主”托管对象上下文,该上下文通常由应用程序委托拥有。 So long as you can get the app delegate (hint: [[UIApplication sharedApplication] delegate] ) you have access to the managed object context. 只要您可以获得应用程序委托(提示: [[UIApplication sharedApplication] delegate] ),您就可以访问托管对象上下文。 I like to define a static global variable to hold a reference to my app delegate to make life easier. 我喜欢定义一个静态全局变量来保存对我的app委托的引用,以使生活更轻松。

There's generally a one-to-one correspondence between NSFetchedResultsController instances and UITableView instances. NSFetchedResultsController实例和UITableView实例之间通常存在一对一的对应关系。 Aside from populating table views, it's exceedingly rare that you would need an NSFetchedResultsController . 除了填充表视图外,您需要一个NSFetchedResultsController是非常罕见的。 If you have a number of similar views (eg a tab bar that lets you view the same data in different ways a la the iPod app), it would behoove you to create a single base class that configures the NSFetchedResultsController and derive your specific view controllers from that. 如果您有许多类似的视图(例如,一个标签栏可以让您以不同的方式查看iPod应用程序的相同数据),那么您应该创建一个配置NSFetchedResultsController的单个基类并派生您的特定视图控制器从那以后。

Now, when you create view controllers to edit an object, it's generally a good idea to do that in a separate managed object context. 现在,当您创建视图控制器以编辑对象时,通常最好在单独的托管对象上下文中执行此操作。 If the user cancels, you just discard the context and the changes go away. 如果用户取消,您只需丢弃上下文,更改就会消失。 Again, you don't really need an NSFetchedResultsController for this because these views are only concerned with a single object. 同样,您实际上并不需要NSFetchedResultsController ,因为这些视图仅涉及单个对象。

When you're done editing, you save: the managed object context. 完成编辑后,您save:托管对象上下文。 The objects that manage your other managed object contexts should implement the NSFetchedResultsControllerDelegate methods to keep the table view in sync. 管理其他托管对象上下文的对象应实现NSFetchedResultsControllerDelegate方法以使表视图保持同步。 Again, this can be implemented in a base class so you can generalize this functionality for related view controllers. 同样,这可以在基类中实现,因此您可以为相关的视图控制器概括此功能。

Do you absolutely have to use a CoreData model, or would something using a NSCoder (NSArchiver, NSKeyedArchiver, etc) work? 你绝对必须使用CoreData模型,还是使用NSCoder(NSArchiver,NSKeyedArchiver等)工作? I've found that CoreData is overkill for most applications. 我发现CoreData对大多数应用程序来说都是过度杀伤力。

Also, could you clarify why you can't take an approach using singletons? 另外,你能澄清为什么你不能采用单身人士的方法吗? I've used singleton factories in a number of applications without issues. 我在许多应用程序中使用了单件工厂而没有任何问题。 It's fairly easy to define class-level methods that operate on a shared (singleton) instance. 定义在共享(单例)实例上运行的类级方法相当容易。

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

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