- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
最近我了解到“您真的不应该调用 AppDelegate 来获取托管对象上下文”。 Apple 也已将此建议放入其文档中 here .它是这样的:
A view controller typically shouldn’t retrieve the context from a global object such as the application delegate—this makes the application architecture rigid. Neither should a view controller create a context for its own use (unless it’s a nested context). This may mean that operations performed using the controller’s context aren’t registered with other contexts, so different view controllers will have different perspectives on the data.
此外,他们还提到了其他一些获取上下文的方法。到目前为止,我无法弄清楚他们在那里想说什么。任何人都可以对这个问题有所了解。任何支持语句的代码片段都将受到欢迎。
编辑
Sometimes, though, it’s easier or more appropriate to retrieve the context from somewhere other than application or the document, or the view controller. Several objects you might use in a Core Data-based application keep a reference to a managed object context. A managed object itself has a reference to its own context, as do the various controller objects that support Core Data such as array and object controllers (NSArrayController and NSObjectController in OS X, and NSFetchedResultsController in iOS).
Retrieving the context from one of these objects has the advantage that if you re-architect your application, for example to make use of multiple contexts, your code is likely to remain valid. For example, if you have a managed object, and you want to create a new managed object that will be related to it, you can ask original object for its managed object context and create the new object using that. This will ensure that the new object you create is in the same context as the original.
它到底是什么?我确信它与下面的高度投票答案不相似。有人可以帮助我理解这部分 Apple 文档吗?
最佳答案
这叫做依赖注入(inject)。基本上,调用者/构造者应该将 NSManagedObjectContext
设置到被调用者/构造者上。
在您的 AppDelegate
中,您应该将 NSManagedObjectContext
设置到与 UIWindow
关联的 rootViewController
中。
然后,您的 rootViewController
应该将 NSManagedObjectContext
设置到下一个 View Controller 中,依此类推。
如何?它只是 View Controller 类上的一个简单属性,调用者使用:
[nextViewController setManagedObjectContext:[self managedObjectContext]];
有些人可能会推荐单例,但这是另一个最好避免的深坑。
依赖注入(inject)是最好的方法。
这是 Apple 设计的方法。另一种选择涉及某种形式的单例:AppDelegate 或其他单例。
"The downside of passing the same context between controllers is that if a same entity is modified in two different places, you have to manage the merge conflict."
那是一个完全不同的问题,不会用多个 NSManagedObjectContext
实例来解决。事实上,多个实例会使情况变得更糟并保证合并冲突。
在那种情况下,您的 View Controller 应该监听托管对象的变化并对其使用react。使得无法在 UI 中同时在两个位置更新它。用户根本无法同时关注两个地方,因此第二个位置将实时更新。
这是该问题的正确答案。
将两个实体置于同一上下文中将确保其正常工作。多个上下文将导致它们成为内存中具有相同数据的两个对象,并且如果不保存到上下文就无法注意到更改。
但是,如果您的 View Controller 在没有用户干预的情况下修改数据,那么您就会遇到一个单独的问题。 View Controller 供用户修改或查看数据。它们不是任何类型的数据后台处理的地方。
如果您处于重要情况,那么这与您提出的问题不同。在那种情况下,您(应该)使用多个线程(UI 线程、导入线程)并且您必须每个至少有一个上下文。
在那种情况下,您确实有合并冲突的风险,您需要为发生的情况编写代码。第一步是更改 NSManagedObjectContext
实例的合并策略。
我怀疑您误读了该文档。
描述的是从 NSManagedObject
实例中获取 NSManagedObjectContext
的能力。这绝对有用。以能够添加或编辑对象的 View Controller 为例。通过将 只是 NSManagedObject
推送到 View Controller ,您可以控制和决定 View Controller 将要触及的内容。接收 View Controller 知道它需要允许编辑接收到的 NSManagedObject
。它不关心正在使用什么 NSManagedObjectContext
。它可以与主体一起工作,它可以与 child 一起工作,它可以在单元测试中被隔离,它不需要知道或关心。如果用户选择保存编辑,它只是显示来自 NSManagedObject
的数据,并保存关联的 NSManagedObjectContext
。
该文档并不建议为您的 NSManagedObjectContext
提供一些通用位置(也称为单例)。这表明,如果您有另一种方法来访问与 NSManagedObject
关联的 NSManagedObjectContext
,那么这样做是可以的,而且这样做绝对有意义。
关于ios - 除了从 appDelegate 获取之外,如何获取 viewController 的 managedObjectContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21050408/
因此,我有 CoreData 实体 Book、Bookmark、Highlight。一本书包含关于这本书的信息和一个 Bookmarks 的 NSSet。 我想删除一本书中的书签,然后立即用我的集合重
(如果这个问题有点令人困惑/不精确,我很抱歉。我只是在学习高级 CoreData 用法,我不太了解术语和其他东西)。 我有一个单例 Game,它包含您在游戏过程中需要的某些数据。例如,您可以从那里访问
我有一个基本的 SwiftUI 应用程序,它初始化了 NSPersistentCloudKitContainer在应用程序启动时基于持久堆栈并将 viewContext 注入(inject)环境并将其
我正在将 Core Data 集成到不是使用 Core Data 模板创建的应用程序中。我对此有疑问,可能有一个非常简单的答案。然而,由于大多数关于这个主题的教程都是针对使用模板的,所以我很难过。 我
多个 ManagedObjectContext (MOC) 如何在核心数据中工作(Swift 2、iOS 9)。我浏览了很多在线链接和 Material 以及 StackOverflow 上的答案,但
我正在尝试创建一个新的核心数据对象,并用某个 View 的 subview 的一些信息填充它。 (只是为了用 template_control 对象填充数组) 有什么问题?该对象无法保存,但稍后当我对
我有两个 ManagedObjectContext(MOC):一个临时MOC 和一个持久MOC。如果我在 tempMOC 中初始化一个实体并且用户决定保存,我将保存更改并将更改合并到 persiste
我正在尝试学习核心数据,但在将新对象添加到现有对象后更新 MOC 时遇到问题。我可以创建原始对象(训练日),并且可以将练习对象添加到该训练日,但我不知道如何保存上下文,以便稍后在我的应用程序中可以找到
非常需要建议,目前没有想法。我与核心数据并发相关的问题叠加在一起,调试我使用 -"com.apple.CoreData.ConcurrencyDebug"和我所拥有的: 堆栈: 线程 3 队列:cor
我正在按照教程开发 iOS 应用程序。我正在使用核心数据。应用程序的第一个 View 是 RootViewController。所有核心数据堆栈都在 AppDelegate 文件中。这是 AppDel
我目前正在开发一个发出 HTTP 请求(通过 AsiHTTPRequest)然后解析 JSON 流的应用程序。基本上有一个 tableView/NSFetchedResultsController,它
我有一个应用程序,它使用 CoreData 使用 MPMediaPicker 保存不同的播放列表。保存第一个后,当我尝试添加第二个时,应用程序崩溃了。下面是整个调试队列。 创建第一条记录。 music
如何管理临时保存的 CoreData?一旦我做了这样的事情: var myClass: MyClass = NSEntityDescription.insertNewObjectForEntityFo
在我正在开发的应用程序中,我需要同时访问和写入核心数据。我已经能够收集到这意味着我需要使用多个 managedObjectContexts,但我不明白我应该如何设置这两个 managedObjectC
我是 iOS 开发新手。我正在编写一个允许用户读取/写入核心数据记录的 iOS 应用程序。这些记录将通过 http 同步到服务器。我有一组链式(串行)NSOperations 在执行同步的后台线程中运
我正在尝试将核心数据添加到现有项目中。我有: 1) 添加了核心数据框架2)将访问器和属性添加到 AppDelegate3)创建数据模型文件 现在当我尝试调用NSManagedObjectContext
我想使用单例 UIApplication 来访问 AppDelegate 的 managedObjectContext。但是当我写 [[[UIApplication sharedApplication
在我的 iOS 应用程序中,我有一个核心数据,我注意到有时在特定 View 中,当我从核心数据中检索信息时,并不总是最新的,我解释得很好: 如果我更新核心数据中的一些值,然后进入特定 View 查看此
我在 iPhone 上的核心数据项目遇到问题。当用户开始添加对象,然后在此过程中取消时,就会发生这种情况。 如果用户点击取消并返回到对象列表,则会在那里列出一个虚拟对象,代表他们正在创建的对象。这永远
我有两个实体,我将它们建模为类和核心数据模型实体/这些类如下所示: 类:StateManager #import #import #import "StateManager.h" #import
我是一名优秀的程序员,十分优秀!