传递managedObjectContext - 这是高效吗? [英] Passing managedObjectContext - is this efficient?

查看:96
本文介绍了传递managedObjectContext - 这是高效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我正在开发的应用程序。有一个导航控制器,我的appDelegate传递其managedObjectContext到nav控制器根控制器,如下:

I have an app I am working on. There is a navigation controller, and my appDelegate is passing its managedObjectContext to the nav controllers root controller like so:

RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
rootViewController.managedObjectContext = self.managedObjectContext;

(奖金问题 - 我已阅读上述内容是传递上下文的首选方式,而不是一些例子我看到视图控制器从代理中抓取上下文 - 这是正确的?此外,我需要发布上面的rootViewController,或者我是正确的,因为它是自动释放,因为它不是使用alloc创建的,新的?)

(Bonus question - I've read the above is the preferred way to pass the context, as opposed to some examples I see where the view controller grabs the context from the delegate - is this correct? Also, do I need to release rootViewController above, or am I correct in that it is autoreleased because it was not created using alloc or new?)

无论如何,应用程序的主视图有一个按钮 - 当点击时,它记录一个时间戳并将其保存在核心数据。然后,应用程序显示第二个视图,这是用户要使用数据子集的地方。第二个视图允许用户选择查看当前周,月或年的所有时间戳。

Anyway, the main view of the app has a button - when clicked, it records a timestamp and saves it in core data. The app then displays a second view, which is where the user is going to work with a subset of the data. This second view allows the user to choose to see all timestamps from the current week, month, or year.

我最初的想法是将managedObjectContext从rootVC传递到detailVC,并在detailVC中执行数据读取和查询。这是正确的方法去做这件事,或者是更好地在rootVC执行查询,并将数据传递给细节控制器作为数组或东西?或者除了组织性之外没有什么区别(没有性能差异) - 其中6个,其中6个。

My initial thought is to pass the managedObjectContext from the rootVC to the detailVC, and perform the data read and queries in the detailVC. Is this the proper way to go about this, or is it better to perform the query at the rootVC and pass the data to the detail controller as an array or something? Or does it make no difference other than organizationally (no performance difference) - 6 of one, half a dozen of the other?

推荐答案

你真的不必担心效率,它会执行精细无论哪种方式你做的 - 你只是传递一个指针。这不是说,这是一个体面的方式去处理事情,

You don't really have to worry about the efficiency of this, it will perform fine whichever way you do it - you're just passing a pointer around. That's not to say this is a decent way of going about things though.

苹果的核心数据模板是非常可怕的。一般来说,最好将您的数据管理分解成一个单独的管理员类,负责管理您的数据。

Apple's Core Data templates are pretty terrible. Generally speaking, it's better to factor out your data management into a separate manager class that has responsibility for managing your data.

这篇关于传递managedObjectContext - 这是高效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆