gpt4 book ai didi

ios - 使用 AppDelegate 为核心数据定义上下文作为单例

转载 作者:行者123 更新时间:2023-11-28 07:29:31 27 4
gpt4 key购买 nike

我正在努力了解核心数据的 NSManagedObjectContext。如果在创建新项目时选中 Core Data 复选框,Xcode 10.1 会提供大量样板。但是我发现如何为每个 View Controller 设置当前上下文有点令人困惑。我想我有更好的方法,正在寻找建议来确认,或者让我回到正确的轨道上。

例如,在样板 AppDelegate 代码中,didFinishLaunchingWithOptions 像这样向 MasterViewController 提供上下文:

let masterNavigationController = splitViewController.viewControllers[0] as! UINavigationController
let controller = masterNavigationController.topViewController as! MasterViewController
controller.managedObjectContext = self.persistentContainer.viewContex

在 MasterViewContoller 中,context 的第一次使用是从 fetchedResultsController 中获取它并且有代码来保存所提供的上下文,即使 AppDelegate 已经有一个 saveContext() 函数可以用来做同样的事情:

@objc
func insertNewObject(_ sender: Any) {
let context = self.fetchedResultsController.managedObjectContext
let newEvent = Event(context: context)

// If appropriate, configure the new managed object.
newEvent.timestamp = Date()

// Save the context.
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}

在我的具有多个 View Controller 的应用程序中,我在尝试重新声明或移交每个 View Controller 中需要的上下文时犯了错误,因此不得不应对由于无意中有多个上下文飞来飞去而导致的错误。

所以我的问题是:我是在犯错,还是以下方法有一些缺点:

1) 使 AppDelegate 成为单例:

class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {

var window: UIWindow?

static let shared = AppDelegate()

2) 在每个需要的类中,始终定义上下文(我假设我只需要一个),如下所示:

let context = AppDelegate.shared.persistentContainer.viewContext

3) 每当需要保存上下文时,按如下方式进行:

AppDelegate.shared.saveContext()

这看起来更简单、更清晰并且更不容易出错,并且似乎在我的实现中起作用。有没有我没有看到的问题?

最佳答案

老实说,Apple 示例/模板对于初学者来说总是不好的例子,因为它们只显示一件事并且在休息时“破解”(例如强制展开所有内容)。初学者往往只是复制这种方法。

免责声明:我谈论的是中大型应用程序。您总是可以在小应用程序中违反这些规则和建议,因为不使用它们会更容易并导致更简单的应用程序。

使 AppDelegate 成为单例:

在 99% 的情况下,你不应该自己实例化 AppDelegate。它通过 UIApplication/@UIApplicationMain 注释为您处理。

AppDelegate 已经是单例,因为每个应用程序在整个生命周期中只有一个委托(delegate)。您可以通过 UIApplication.shared.delegate 访问它吗? AppDelegate

但你不应该。 AppDelegate 通过为系统和你的代码之间的通信提供入口点在每个应用程序中扮演特定角色,你不应该添加额外的角色它(作为句柄数据库)。在大多数情况下,在代码库中的某个地方访问它会出现代码异味和糟糕的架构。

分离CoreData栈

数据库访问是很好地使用单例模式的少数例子之一。但是,不要使用 AppDelegate,您应该创建单独的服务,该服务仅负责处理与核心数据的通信(例如创建和处理堆栈、发送查询等)。

所以 CoreDataService 是个不错的选择。

访问核心数据

使用单例并不意味着您可以通过键入 Singleton.shared 在任何地方访问它。这将大大降低组件的可测试性,并使它们与单例高度耦合。

相反,您应该阅读 Dependency injection principle并注入(inject)你的单例。例如:

class MyViewController: UIViewController {
let dataBaseManager: CoreDataService
init(with dataBaseManager: CoreDataService) {
self.dataBaseManager = dataBaseManager
super.init(nibName: nil, bundle: nil)
}
}

理想情况下,您应该更进一步到 SOLID并仅向 Controller 提供它真正需要的内容:

protocol EventsProvider {
func getEvents(with callback: [Event] -> Void)
}

extension CoreDataService: EventsProvider {
func getEvents(with callback: [Event] -> Void) {
// your core data query here
}
}

class MyViewController: UIViewController {
let eventsProvider: EventsProvider
init(with eventsProvider: EventsProvider) {
self.eventsProvider = eventsProvider
super.init(nibName: nil, bundle: nil)
}
}

let vc = MyViewController(with: CoreDataService.shared)

多个上下文

拥有多个 NSManagedObjectContext 可以很方便并提高性能,但前提是您知道如何使用它们。
这是更高级的话题,所以你现在可以忽略它。
您可以在 Core Data Programming Guide 中阅读相关信息

关于ios - 使用 AppDelegate 为核心数据定义上下文作为单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55308886/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com