I have created a separate CoreDataStack.swift object outside of the AppDelegate to follow much of the advice on here when working with CoreData. But how do I pass the MOC to the rootViewController in AppDelegate? Do I need to add code to the AppDelegate or to the ViewController? Thanks to anyone who can help me here in Swift as much of the older ObjC doesn't work for me!
我创建了一个独立的CoreDataStack。在使用CoreData时,在AppDelegate之外使用swift对象来遵循这里的大部分建议。但是如何将MOC传递给AppDelegate中的rootViewController呢?我需要向AppDelegate或ViewController添加代码吗?感谢所有能在这里迅速帮助我的人,因为大多数老物件对我不起作用!
my CoreDataStack includes the MOC
我的CoreDataStack包括MOC
lazy var managedObjectContext: NSManagedObjectContext = {
let managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType) //
managedObjectContext.persistentStoreCoordinator = self.persistentStoreCoordinator
return managedObjectContext
}()
The AppDelegate has a method to create some data and in didFinishLaunchingWithOptions I can print to the console to see its working
AppDelegate有一个方法来创建一些数据,在didFinishLaunchingWithOptions中,我可以打印到控制台以查看它的工作情况
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let fetchRequest = NSFetchRequest(entityName: "WordList")
do {
let results = try coreDataStack.managedObjectContext.executeFetchRequest(fetchRequest)
if results.count == 0 {
addTestData()
}
} catch {
fatalError("Error fetching data!")
}
do {
if let results = try coreDataStack.managedObjectContext.executeFetchRequest(fetchRequest) as? [NSManagedObject] { //cast to an array of NSManagedObject
for result in results {
if let listName = result.valueForKey("listName") as? String {
print("Got a '\(listName)'")
}
}
}
} catch {
print("There was a fetch error")
}
// WHAT CODE HERE WILL PASS THE MOC TO THE ROOT VC?
return true
}
I have a simple TableViewController embedded in a navigation controller
我有一个简单的TableViewController嵌入到导航控制器中
import UIKit
import CoreData
class WordListsTableViewController: UITableViewController {
var coreDataStack: CoreDataStack!
var wordLists = [WordList]()
override func viewDidLoad() {
super.viewDidLoad()
title = "Word Lists"
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "viewWordList")
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// DO I NEED CODE HERE TO CALL THE MOC?
reloadData()
tableView.reloadData()
}
func reloadData() {
let fetchRequest = NSFetchRequest(entityName: "WordList")
do {
if let results = try coreDataStack.managedObjectContext.executeFetchRequest(fetchRequest) as? [WordList] {
wordLists = results
}
} catch {
fatalError("There was an error fetching wordLists!")
}
}
1 个解决方案
#1
2
There are several ways to access managed object context. If you defined managedObjectContext
in the AppDelegate you can use:
有几种访问托管对象上下文的方法。如果在AppDelegate中定义managedObjectContext,可以使用:
let appDelegate = UIApplicationDelegate.sharedApplication().delegate as! AppDelegate
let managedObjectContext = appDelegate.managedObjectContext
managedObjectContext.doStuff()
The second way to pass managedObjectContext
through view controllers is to create an extension of UIViewController
.
通过视图控制器传递managedObjectContext的第二种方式是创建一个UIViewController的扩展。
extension UIViewController {
lazy var managedObjectContext: NSManagedObjectContext {
// Create core data stack or use singleton object
return coreDataStack.managedObjectContext
}
}
By doing this would be able to access managedObjectContext on each subclass of UIViewController
. Then you can write:
这样做可以访问UIViewController的每个子类上的managedObjectContext。然后你可以写:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.managedObjectContext.doSomething()
}
By accessing managedObjectContext in any of these ways you are able to get managedObjectContext
in your rootViewController
and in any UIViewController
you create.
通过使用这些方法访问managedObjectContext你可以在你的rootViewControllerand在你创建的任何UIViewController中获得managedObjectContext。
#1
2
There are several ways to access managed object context. If you defined managedObjectContext
in the AppDelegate you can use:
有几种访问托管对象上下文的方法。如果在AppDelegate中定义managedObjectContext,可以使用:
let appDelegate = UIApplicationDelegate.sharedApplication().delegate as! AppDelegate
let managedObjectContext = appDelegate.managedObjectContext
managedObjectContext.doStuff()
The second way to pass managedObjectContext
through view controllers is to create an extension of UIViewController
.
通过视图控制器传递managedObjectContext的第二种方式是创建一个UIViewController的扩展。
extension UIViewController {
lazy var managedObjectContext: NSManagedObjectContext {
// Create core data stack or use singleton object
return coreDataStack.managedObjectContext
}
}
By doing this would be able to access managedObjectContext on each subclass of UIViewController
. Then you can write:
这样做可以访问UIViewController的每个子类上的managedObjectContext。然后你可以写:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.managedObjectContext.doSomething()
}
By accessing managedObjectContext in any of these ways you are able to get managedObjectContext
in your rootViewController
and in any UIViewController
you create.
通过使用这些方法访问managedObjectContext你可以在你的rootViewControllerand在你创建的任何UIViewController中获得managedObjectContext。