在后台线程中初始化核心数据堆栈是一个好的设计选择吗?

发布于 2024-10-24 18:59:39 字数 698 浏览 1 评论 0原文

Stackoverflow 用户大家好,

我正在尝试优化我的 iphone 应用程序启动时间(即,在应用程序打开后尽快显示第一个视图)。我想要优化的部分是应用程序委托中核心数据堆栈(托管对象上下文、持久存储协调器等)的初始化。我想了两种方法来解决这个问题,想知道您对这些解决方案(或您可能有的其他解决方案)有何想法(优点/缺点)?

注意:由于在 Cocoa/Cocoa Touch 应用程序中放置“核心数据堆栈”的位置

解决方案 1(我更喜欢的一个):< /强>

从应用程序委托的后台线程中初始化核心数据堆栈。这将使应用程序初始化更快,并且窗口/视图更快显示。

解决方案 2:

当视图控制器需要访问核心数据堆栈时,延迟实例化核心数据堆栈。然而,我喜欢这个想法,核心数据堆栈应该在应用程序委托中初始化,然后在需要它的每个视图控制器中传递。

注意:此解决方案打破了在应用程序委托中初始化核心数据堆栈的约定。

问候,

Hi fellow Stackoverflow users,

I am trying to optimize my iphone application startup time (i.e make the first view appear as soon as possible once the app is open). The section I want to optimize is the initialization of the Core Data stack (managed object context, persistent store coordinatorm, etc...) in the application delegate. I thought of two ways to solve this problem and wonder what are you thoughts(pros/cons) on those solutions (or other you may have) ?

Note: The Core Data Stack is initialized in the application delegate for the reasons given in Where to place the "Core Data Stack" in a Cocoa/Cocoa Touch application

Solution 1 (my prefered one):

Initialize the Core Data Stack in a background thread from the application delegate. This will allow the application initialization to be faster and the window/view to be displayed sooner.

Solution 2:

Lazy instantiate the Core Data Stack when a view controller requires access to the Core Data Stack. I like this idea however, the Core Data Stack should be initialized in the application delegate and then be passed on in each view controller which requires it.

Note: This solution breaks the convention of initializing the Core Data Stack in the application delegate.

Regards,

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

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

发布评论

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

评论(1

三人与歌 2024-10-31 18:59:39

如果您查看使用模板的标准 Core Data,您会发现堆栈直到某些视图控制器调用应用程序委托的 ManagedObjectContext 才初始化。因此,直到需要 ManagedObjectContext 的视图加载并开始配置自身之后,核心数据堆栈才会初始化。

堆栈的实际初始化非常轻量且快速。在您开始主动生成对象图并读取和写入持久存储之前,核心数据不会做太多事情。简单地初始化堆栈只需要非常非常少的时间。

不久前我也有类似的担忧并做了一些测试。我发现启动时真正的延迟是第一个视图的加载,无论它是否使用 Core Data。我无法测量启用和禁用核心数据之间启动时间的任何差异。

因此,我不会为非标准初始化而烦恼,除非您自己的自定义测试表明它会导致一些性能拖累。

If you look at the standard Core Data utilizing templates, you will see that the stack is not initialized until some view controller calls for the managedObjectContext of the app delegate. So, the Core Data stack does not initialize until after a view requiring the managedObjectContext loads and begins to configure itself.

The actual initialization of the stack is very lightweight and quick. Core Data doesn't do much until you start actively generating the object graph and reading and writing to persistent store. Simply initializing the stack takes very, very little time.

Sometime back I had similar concerns and did some test. I found out that the real lag in start ups is the loading of the first view, regardless of whether it used Core Data or not. I wasn't able to measure any differences in startup time between having Core Data enabled and having it disabled.

So, I wouldn't bother with a non-standard initialization unless your own custom testing shows it causes some performance drag.

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