I am trying to create a today extension that displays data from the parent app by using a shared app group container, and then adding the persistent store to a context.
Add Today Extension Target
Turn on app groups for parent app and extension and select same group
Add Today Extension as Target Membership for Data model and entities
Add Persistent store to context
Fetch Objects
I get no errors but the extension does not seem to be fetching any results. Does anybody have any suggestions where I may be going wrong ?
Heres what I am doing in the extension TodayViewController
class TodayViewController: UIViewController, NCWidgetProviding {
var context: NSManagedObjectContext!
#IBOutlet weak var table: UITableView!
var objectsArray = [Objects]()
override func viewDidLoad() {
super.viewDidLoad()
let fileManager = NSFileManager.defaultManager()
var containerPath = fileManager.containerURLForSecurityApplicationGroupIdentifier("group.com.Company.AppName")
containerPath = containerPath?.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
let modelURL = NSBundle.mainBundle().URLForResource("AppName", withExtension: "momd")
let model = NSManagedObjectModel(contentsOfURL: modelURL!)
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: model!)
do {
try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: containerPath, options: nil)
} catch {
print("yellow")
}
context = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
context.persistentStoreCoordinator = coordinator
let moc = context
let request = NSFetchRequest(entityName: "Objects")
request.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
do {
try
self.objectsArray = moc.executeFetchRequest(request) as! [Objects]
print ("objects count \(objectsArray.count)")
} catch {
// failure
print("Fetch failed")
}
self.table.reloadData()
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
//return sectionsArray.count
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.objectsArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell!
cell.textLabel!.textColor = UIColor.whiteColor()
cell.textLabel!.text = self.objectsArray[indexPath.row].title
return cell
}
func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
// Perform any setup necessary in order to update the view.
// If an error is encountered, use NCUpdateResult.Failed
// If there's no update required, use NCUpdateResult.NoData
// If there's an update, use NCUpdateResult.NewData
completionHandler(NCUpdateResult.NewData)
}
}
In Apple's App Extension Programming Guide --Sharing Data with Your Containing App
Even though an app extension bundle is nested within its containing app’s bundle, the running app extension and containing app have no direct access to each other’s containers.
So they wouldn't be able to share data directly,even if you
Add Today Extension as Target Membership for Data model and entities
Instead,they communicated with each other by shared container(App groups) indirectly,which you had already setup in step 2.
So the solution is :
In your Contraning App,create the sql data model in your shared container ,insert data in this database.Adjust your code in todayExtension ,and fetch data.
Or Using NSUserDefaults to share data instead.
like this:
// Create and share access to an NSUserDefaults object
NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName: #"com.example.domain.MyShareExtension"];
// Use the shared user defaults object to update the user's account
[mySharedDefaults setObject:theAccountName forKey:#"lastAccountName"];
Related
I am developing a small app to connect to my site, download data via a PHP web service, and display it in a table view. To get started I was following a tutorial over on Medium by Jose Ortiz Costa (Article on Medium).
I tweaked his project and got it running to verify the Web service was working and able to get the data. Once I got that working, I started a new project and tried to pull in some of the code that I needed to do the networking and tried to get it to display in a tableview in the same scene instead of a popup scene like Jose's project.
This is where I am running into some issues, as I'm still rather new to the swift programming language (started a Udemy course and have been picking things up from that) getting it to display in the table view. I can see that the request is still being sent/received, but I cannot get it to appear in the table view (either using my custom XIB or a programmatically created cell). I thought I understood how the code was broken down, and even tried to convert it from a UITableViewController to a UITableviewDataSource via an extension of the Viewcontroller.
At this point, I'm pretty stumped and will continue to inspect the code and tweak what I think might be the root cause. Any pointers on how to fix would be really appreciated!
Main Storyboard Screenshot
Struct for decoding my data / Lead class:
import Foundation
struct Lead: Decodable {
var id: Int
var name: String
var program: String
var stage: String
var lastAction: String
}
class LeadModel {
weak var delegate: Downloadable?
let networkModel = Network()
func downloadLeads(parameters: [String: Any], url: String) {
let request = networkModel.request(parameters: parameters, url: url)
networkModel.response(request: request) { (data) in
let model = try! JSONDecoder().decode([Lead]?.self, from: data) as [Lead]?
self.delegate?.didReceiveData(data: model! as [Lead])
}
}
}
ViewController:
import UIKit
class LeadViewController: UIViewController {
// Buttons
#IBOutlet weak var newButton: UIButton!
#IBOutlet weak var firstContactButton: UIButton!
#IBOutlet weak var secondContactButton: UIButton!
#IBOutlet weak var leadTable: UITableView!
let model = LeadModel()
var models: [Lead]?
override func viewDidLoad() {
super.viewDidLoad()
//Make Buttons rounded
newButton.layer.cornerRadius = 10.0
firstContactButton.layer.cornerRadius = 10.0
secondContactButton.layer.cornerRadius = 10.0
//Delegate
model.delegate = self
}
//Send request to web service based off Buttons Name
#IBAction func findLeads(_ sender: UIButton) {
let new = sender.titleLabel?.text
let param = ["stage": new!]
print ("findLead hit")
model.downloadLeads(parameters: param, url: URLServices.leads)
}
}
extension LeadViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
print ("number of sections hit")
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
guard let _ = self.models else {
return 0
}
print ("tableView 1 hit")
return self.models!.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Create an object from LeadCell
let cell = tableView.dequeueReusableCell(withIdentifier: "leadID", for: indexPath) as! LeadCell
// Lead selection
cell.leadName.text = self.models![indexPath.row].name
cell.actionName.text = self.models![indexPath.row].lastAction
cell.stageName.text = self.models![indexPath.row].stage
cell.progName.text = self.models![indexPath.row].program
print ("tableView 2 hit")
// Return the configured cell
return cell
}
}
extension LeadViewController: Downloadable {
func didReceiveData(data: Any) {
//Assign the data and refresh the table's data
DispatchQueue.main.async {
self.models = data as? [Lead]
self.leadTable.reloadData()
print ("LeadViewController Downloadable Hit")
}
}
}
EDIT
So with a little searching around (okay...A LOT of searching around), I finally found a piece that said I had to set the class as the datasource.
leadTable.dataSource = self
So that ended up working (well after I added a prototype cell with the identifier used in my code). I have a custom XIB that isn't working right now and that's my next tackle point.
You load the data, but don't use it. First, add the following statement to the end of the viewDidLoad method
model.delegate = self
Then add the following LeadViewController extension
extension LeadViewController: Downloadable {
func dicReceiveData(data: [Lead]) {
DispatchQueue.main.async {
self.models = data
self.tableView.reloadData()
}
}
}
And a couple of suggestions:
It is not a good practice to use the button title as a network request parameter:
let new = sender.titleLabel?.text
let param = ["stage": new!]
It is better to separate UI and logic. You can use the tag attribute for buttons (you can configure it in the storyboard or programmatically) to check what button is tapped.
You also have several unnecessary type casts in the LeadModel class. You can change
let model = try! JSONDecoder().decode([Lead]?.self, from: data) as [Lead]?
self.delegate?.didReceiveData(data: model! as [Lead])
to
do {
let model = try JSONDecoder().decode([Lead].self, from: data)
self.delegate?.didReceiveData(data: model)
}
catch {}
There is an option in my app to add new excercises (via user input), i've managed to set the alert and add feature, but I can't save the data added and let it be persistent after closure and that.
//
// ExcerciseListViewController.swift
// OneRepMax
//
// Created by Mauro Garcia on 17/01/2019.
// Copyright © 2019 Mauro Garcia. All rights reserved.
//
import Foundation
import UIKit
import CoreData
class ExcerciseListViewController: UIViewController, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
title = "Excercises"
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
// This view controller itself will provide the delegate methods and row data for the table view.
tableView.delegate = self
tableView.dataSource = self
}
#IBOutlet weak var tableView: UITableView!
// Add a new excercise
#IBAction func addExcercise(_ sender: UIBarButtonItem) {
let alert = UIAlertController(title: "New Excercise", message: "Add a new excercise", preferredStyle: .alert)
let saveAction = UIAlertAction(title: "Save", style: .default) { [unowned self] action in
guard let textField = alert.textFields?.first,
let nameToSave = textField.text
else {
return
}
print(nameToSave)
self.excercises.append(nameToSave)
self.tableView.reloadData()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
alert.addTextField()
alert.addAction(saveAction)
alert.addAction(cancelAction)
present(alert, animated: true)
}
var excercises: [String] = []
}
// MARK: - UITableViewDataSource
extension ExcerciseListViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
print(excercises.count)
return excercises.count
}
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath)
-> UITableViewCell {
let cell =
tableView.dequeueReusableCell(withIdentifier: "Cell",
for: indexPath)
cell.textLabel?.text = excercises[indexPath.row]
return cell
}
// What happens when user touch an excercise
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let excerciseChosen = "\(excercises[indexPath.row])"
goBackToOneButtonTapped((Any).self)
excUserChose = excerciseChosen
print(excUserChose)
print("You tapped cell number \(indexPath.row).")
print(excerciseChosen)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeText"), object: nil)
}
func goBackToOneButtonTapped(_ sender: Any) {
performSegue(withIdentifier: "unwindToViewController", sender: self)
}
}
// TODO: save user input
In my AppDelegate.swift
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "OneRepMax")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// 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.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
// MARK: - Core Data Saving support
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
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)")
}
}
}
I want to -when the user press "Save"- to save the data to be accesible after.
I've tried to implement CoreData but I have failed trying. Don't know what I'm doing wrong.
EDIT:
Code for adding and saving input
var excercisess: [NSManagedObject] = []
#IBAction func addExcercise(_ sender: UIBarButtonItem) {
let alert = UIAlertController(title: "New Excercise", message: "Add a new excercise", preferredStyle: .alert)
let saveAction = UIAlertAction(title: "Save", style: .default) { [unowned self] action in
guard let textField = alert.textFields?.first,
let nameToSave = textField.text
else {
return
}
self.save(name: nameToSave)
self.tableView.reloadData()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
alert.addTextField()
alert.addAction(saveAction)
alert.addAction(cancelAction)
present(alert, animated: true)
}
func save(name: String) {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
/*1.
Before you can save or retrieve anything from your Core Data store, you first need to get your hands on an NSManagedObjectContext. You can consider a managed object context as an in-memory “scratchpad” for working with managed objects.
Think of saving a new managed object to Core Data as a two-step process: first, you insert a new managed object into a managed object context; then, after you’re happy with your shiny new managed object, you “commit” the changes in your managed object context to save it to disk.
Xcode has already generated a managed object context as part of the new project’s template. Remember, this only happens if you check the Use Core Data checkbox at the beginning. This default managed object context lives as a property of the NSPersistentContainer in the application delegate. To access it, you first get a reference to the app delegate.
*/
let managedContext = appDelegate.persistentContainer.viewContext
/*
An NSEntityDescription object is associated with a specific class instance
Class
NSEntityDescription
A description of an entity in Core Data.
Retrieving an Entity with a Given Name here person
*/
let entity = NSEntityDescription.entity(forEntityName: "Excercise", in: managedContext)!
/*
Initializes a managed object and inserts it into the specified managed object context.
init(entity: NSEntityDescription,
insertInto context: NSManagedObjectContext?)
*/
let excercises = NSManagedObject(entity: entity, insertInto: managedContext)
//we can simply create person object this way also.
// let excercise = Excercise(context: managedContext)
/*
With an NSManagedObject in hand, you set the name attribute using key-value coding. You must spell the KVC key (name in this case) exactly as it appears in your Data Model
*/
excercises.setValue(name, forKeyPath: "name")
/*
You commit your changes to person and save to disk by calling save on the managed object context. Note save can throw an error, which is why you call it using the try keyword within a do-catch block. Finally, insert the new managed object into the people array so it shows up when the table view reloads.
*/
do {
try managedContext.save()
excercisess.append(excercises)
tableView.reloadData()
print("SAVED")
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
And the code for fetching:
func fetchAllExcercises(){
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
/*Before you can do anything with Core Data, you need a managed object context. */
let managedContext = appDelegate.persistentContainer.viewContext
/*As the name suggests, NSFetchRequest is the class responsible for fetching from Core Data.
Initializing a fetch request with init(entityName:), fetches all objects of a particular entity. This is what you do here to fetch all Person entities.
*/
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Excercise")
/*You hand the fetch request over to the managed object context to do the heavy lifting. fetch(_:) returns an array of managed objects meeting the criteria specified by the fetch request.*/
do {
excercisess = try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}
There are several things that you still need once you have the xcdatamodeld file set up.
Your array should be an array of your CoreData entity.
var exercises = [Exercise]()
You need to create an instance of the managed object context.
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
Then you will need to use that instance to create new objects in the array, save the new objects and fetch them. Have a go at finding out how to create those functions (Youtube, web articles) and then see if you still have questions.
I have a question about Swift 3 releted with Core Data. I am developing an App in Swift with Xcode 8, and I need to support iOS 9 and iOS10. The problem is that I don't know how to get the AppDelegate and the Context (to use for store and get data from my Entities). I think my code should be something like this:
#if avaliable(iOS10,*)
{
// iOS 10 code
} else
{
// iOS 9 code
}
But I don't know what to do.
Any idea?
(A small help in correction would be appreciated)
# Code in Swift 3 for Core Data for both iOS 9 and iOS 10 #
Since you want the core data code for both iOS 9 and iOS 10 then you don't have to use NSPersistentContainer as it not supported in iOS 9, so you have to use old method instead
If at the time of project creation you haven't included core data and later you want to include it, follow the following steps :-
Step 1. Go to Build Phases -> Link Binary with Library -> click on + sign -> Add CoreData.framework
Step 2. Now got to file -> New File -> select Data Model.
Step 3. Now you need to write some code inside AppDelegate.swift to get set go :-
import UIKit
import CoreData
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
self.saveContext()
}
// MARK: - Core Data stack
lazy var applicationDocumentsDirectory: NSURL = {
// The directory the application uses to store the Core Data store file. This code uses a directory named "hacker.at.work.mTirgger" in the application's documents Application Support directory.
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return urls[urls.count-1] as NSURL
}()
lazy var managedObjectModel: NSManagedObjectModel = {
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
let modelURL = Bundle.main.url(forResource: "Model", withExtension: "momd")!
return NSManagedObjectModel(contentsOf: modelURL)!
}()
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
// The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.appendingPathComponent("SingleViewCoreData.sqlite")
var failureReason = "There was an error creating or loading the application's saved data."
do {
try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
} catch {
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject?
dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject?
dict[NSUnderlyingErrorKey] = error as NSError
let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() 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.
NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
abort()
}
return coordinator
}()
lazy var managedObjectContext: NSManagedObjectContext = {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
let coordinator = self.persistentStoreCoordinator
var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator
return managedObjectContext
}()
// MARK: - Core Data Saving support
func saveContext () {
if managedObjectContext.hasChanges {
do {
try managedObjectContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// abort() 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
NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
abort()
}
}
}
}
That's all your Core Data is ready for both iOS 9 and iOS 10 in Swift 3. Enjoy !!!
If you want to support iOS9 then the best bet is not to use any new iOS10 stuff.
Stick with the iOS9 API for now, and in the future when you eventually drop iOS9 support you can refactor your code to use the newer methods then.
edit: Just to be clear, all the iOS9 code will run fine on iOS10. You don't need to do anything special there, it is all backwards compatible.
CoreData for iOS9 and iOS10 simultaneously
For who you may need it:
This is an unmodified Master-Detail App template from the latest Xcode7 (for iOS9) release of May 2016. I have updated it with the current latest Xcode (Version 8.2.1)
With this template you can develope an application that uses CoreData compatible with iOS9 and iOS10, at least for now (May 2017)
AppDelegate.swift
//
// AppDelegate.swift
// trash
//
// Created by Markus on 22/05/17.
// Copyright © 2017 Markus. All rights reserved.
//
import UIKit
import CoreData
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let splitViewController = self.window!.rootViewController as! UISplitViewController
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
splitViewController.delegate = self
let masterNavigationController = splitViewController.viewControllers[0] as! UINavigationController
let controller = masterNavigationController.topViewController as! MasterViewController
controller.managedObjectContext = self.managedObjectContext
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
self.saveContext()
}
// MARK: - Split view
func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController:UIViewController, onto primaryViewController:UIViewController) -> Bool {
guard let secondaryAsNavController = secondaryViewController as? UINavigationController else { return false }
guard let topAsDetailController = secondaryAsNavController.topViewController as? DetailViewController else { return false }
if topAsDetailController.detailItem == nil {
// Return true to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
return true
}
return false
}
// MARK: - Core Data stack
lazy var applicationDocumentsDirectory: URL = {
// The directory the application uses to store the Core Data store file. This code uses a directory named "com.senbei.trash" in the application's documents Application Support directory.
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return urls[urls.count-1]
}()
lazy var managedObjectModel: NSManagedObjectModel = {
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
let modelURL = Bundle.main.url(forResource: "trash", withExtension: "momd")!
return NSManagedObjectModel(contentsOf: modelURL)!
}()
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
// The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.appendingPathComponent("SingleViewCoreData.sqlite")
var failureReason = "There was an error creating or loading the application's saved data."
do {
try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
} catch {
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject?
dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject?
dict[NSUnderlyingErrorKey] = error as NSError
let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() 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.
NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
abort()
}
return coordinator
}()
lazy var managedObjectContext: NSManagedObjectContext = {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
let coordinator = self.persistentStoreCoordinator
var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator
return managedObjectContext
}()
// MARK: - Core Data Saving support
func saveContext () {
if managedObjectContext.hasChanges {
do {
try managedObjectContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// abort() 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
NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
abort()
}
}
}
}
MasterViewController.swift
//
// MasterViewController.swift
// trash
//
// Created by Markus on 22/05/17.
// Copyright © 2017 Markus. All rights reserved.
//
import UIKit
import CoreData
class MasterViewController: UITableViewController, NSFetchedResultsControllerDelegate {
var detailViewController: DetailViewController? = nil
var managedObjectContext: NSManagedObjectContext? = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:)))
self.navigationItem.rightBarButtonItem = addButton
if let split = self.splitViewController {
let controllers = split.viewControllers
self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
}
}
override func viewWillAppear(_ animated: Bool) {
self.clearsSelectionOnViewWillAppear = self.splitViewController!.isCollapsed
super.viewWillAppear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func insertNewObject(_ sender: AnyObject) {
let context = self.fetchedResultsController.managedObjectContext
let entity = self.fetchedResultsController.fetchRequest.entity!
let newManagedObject = NSEntityDescription.insertNewObject(forEntityName: entity.name!, into: context)
// If appropriate, configure the new managed object.
// Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
newManagedObject.setValue(Date(), forKey: "timeStamp")
// Save the context.
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// abort() 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.
//print("Unresolved error \(error), \(error.userInfo)")
abort()
}
}
// MARK: - Segues
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let object = self.fetchedResultsController.object(at: indexPath)
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
// MARK: - Table View
override func numberOfSections(in tableView: UITableView) -> Int {
return self.fetchedResultsController.sections?.count ?? 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionInfo = self.fetchedResultsController.sections![section]
return sectionInfo.numberOfObjects
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let object = self.fetchedResultsController.object(at: indexPath) as! NSManagedObject
self.configureCell(cell, withObject: object)
return cell
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let context = self.fetchedResultsController.managedObjectContext
context.delete(self.fetchedResultsController.object(at: indexPath) as! NSManagedObject)
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// abort() 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.
//print("Unresolved error \(error), \(error.userInfo)")
abort()
}
}
}
func configureCell(_ cell: UITableViewCell, withObject object: NSManagedObject) {
//cell.textLabel!.text = object.value(forKey: "timeStamp")!.description
cell.textLabel!.text = (object.value(forKey: "timeStamp")! as AnyObject).description
}
// MARK: - Fetched results controller
//var fetchedResultsController: NSFetchedResultsController {
var fetchedResultsController: NSFetchedResultsController<NSFetchRequestResult> {
if _fetchedResultsController != nil {
return _fetchedResultsController!
}
//let fetchRequest = NSFetchRequest()
//let fetchRequest = NSFetchRequest<Event>(entityName: "Event") //another alternative
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Event")
// Edit the entity name as appropriate.
let entity = NSEntityDescription.entity(forEntityName: "Event", in: self.managedObjectContext!)
fetchRequest.entity = entity
// Set the batch size to a suitable number.
fetchRequest.fetchBatchSize = 20
// Edit the sort key as appropriate.
let sortDescriptor = NSSortDescriptor(key: "timeStamp", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: "Master")
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController
do {
try _fetchedResultsController!.performFetch()
} catch {
// Replace this implementation with code to handle the error appropriately.
// abort() 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.
//print("Unresolved error \(error), \(error.userInfo)")
abort()
}
return _fetchedResultsController!
}
//var _fetchedResultsController: NSFetchedResultsController? = nil
var _fetchedResultsController: NSFetchedResultsController<NSFetchRequestResult>?
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
self.tableView.beginUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
switch type {
case .insert:
self.tableView.insertSections(IndexSet(integer: sectionIndex), with: .fade)
case .delete:
self.tableView.deleteSections(IndexSet(integer: sectionIndex), with: .fade)
default:
return
}
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .insert:
tableView.insertRows(at: [newIndexPath!], with: .fade)
case .delete:
tableView.deleteRows(at: [indexPath!], with: .fade)
case .update:
self.configureCell(tableView.cellForRow(at: indexPath!)!, withObject: anObject as! NSManagedObject)
case .move:
tableView.moveRow(at: indexPath!, to: newIndexPath!)
}
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
self.tableView.endUpdates()
}
/*
// Implementing the above methods to update the table view in response to individual changes may have performance implications if a large number of changes are made simultaneously. If this proves to be an issue, you can instead just implement controllerDidChangeContent: which notifies the delegate that all section and object changes have been processed.
func controllerDidChangeContent(controller: NSFetchedResultsController) {
// In the simplest, most efficient, case, reload the table view.
self.tableView.reloadData()
}
*/
}
DetailViewController.swift
//
// DetailViewController.swift
// trash
//
// Created by Markus on 22/05/17.
// Copyright © 2017 Markus. All rights reserved.
//
import UIKit
class DetailViewController: UIViewController {
#IBOutlet weak var detailDescriptionLabel: UILabel!
var detailItem: AnyObject? {
didSet {
// Update the view.
self.configureView()
}
}
func configureView() {
// Update the user interface for the detail item.
if let detail = self.detailItem {
if let label = self.detailDescriptionLabel {
//label.text = detail.value(forKey: "timeStamp")!.description
label.text = (detail.value(forKey: "timeStamp")! as AnyObject).description
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.configureView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Data model is the original from template, as well as storyboard. Both there are not modified.
You can use the new API and get the benefits out of it by doing something like this:
var coreDataManager: CoreDataManagerProtocol!
#if available(iOS10,*)
{
coreDataManager = CoreDataManagerNewStack()
} else
{
coreDataManager = CoreDataManagerOldStack()
}
and then you implement the new CoreData with the new stack into a class:
class CoreDataManagerNewStack: CoreDataManagerProtocol {
var container: NSPersistentContainer
// etc
}
and the old one with the code paster above with the whole code to generate the stack
class CoreDataManagerOldStack: CoreDataManagerProtocol {
lazy var managedObjectContext: NSManagedObjectContext = {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
let coordinator = self.persistentStoreCoordinator
var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator
return managedObjectContext
}()
// etc
}
I'm new to coding with Swift, and I've been using the tutorial from Udemy by Aaron Caines, which has been great. The tutorial he did was using a UITableViewController.
I have an app that uses a single UIViewController (ONLY ONE VIEW and it's not a UITableViewController). I've already loaded CoreData into the build phases. I've been able to verify that the data is saved in the attributes, but for some reason, I can't load the data back into the two text boxes and one image view that I have in the view controller.
I've placed a couple of questions as comments within the code.
It should be as easy as setting up the variables:
#IBOutlet var textName: UITextField!
#IBOutlet var descriptionName: UITextField!
#IBOutlet var imageView: UIImageView!
calling the entity and getting the persistent container ready to load and receive data:
let pc = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
fetching the data
var frc : NSFetchedResultsController = NSFetchedResultsController<NSFetchRequestResult>()
func fetchRequest() -> NSFetchRequest<NSFetchRequestResult> {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "NamedEntity")
// DO I NEED A SORTER IF I'M NOT USING A TABLEVIEW?
//let sorter = NSSortDescriptor(key: "accounttext", ascending: false)
//fetchRequest.sortDescriptors = [sorter]
return fetchRequest
}
func getFRC() -> NSFetchedResultsController<NSFetchRequestResult> {
frc = NSFetchedResultsController(fetchRequest: fetchRequest(), managedObjectContext: pc, sectionNameKeyPath: nil, cacheName: nil)
//OCCASIONALLY THERE'S AN ISSUE WITH THE sectionNameKeyPath.
//THE ERROR INVOLVES TRYING TO "UNWRAP A NIL VALUE".
//IS THERE ANOTHER VALUE I SHOULD BE CONSIDERING?
return frc
}
fetching the data whenever the view loads or appears:
override func viewDidLoad() {
super.viewDidLoad()
frc = getFRC()
frc.delegate = self
do {
try frc.performFetch()
}
catch {
print(error)
return
}
// WHAT DO I USE HERE IF I'M NOT USING A TABLEVIEW?
self.tableView.reloadData()
}
override func viewDidAppear(_ animated: Bool) {
frc = getFRC()
frc.delegate = self
do {
try frc.performFetch()
}
catch {
print(error)
return
}
// WHAT DO I USE HERE IF I'M NOT USING A TABLEVIEW?
self.tableView.reloadData()
}
and then loading it into the appropriate boxes:
//THIS IS WHERE THINGS GET STUCK
// HOW DO I CALL THE ATTRIBUTES OF MY ENTITY AND UPDATE MY VARIABLES IF I'M NOT USING A TABLEVIEW?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! cellAccountTableViewCell
let item = frc.object(at: indexPath) as! Entity
cell.nameText.text = item.accounttext
cell.descriptionText.text = item.amounttext
cell.imageView.image = UIImage(data: (item.image)! as Data)
return cell
}
I only have 2 text boxes and 1 image view. I've spent the last three days scouring dozens of useless forum topics and countless youtube videos for this answer, but it seems that everyone gives a tutorial on using a table view controller.
The most useful thing I've found was a video by Electronic Armory. This helped me understand the structure of the Entity(ies), attrubutes, and the persistentContainer. It also deals with the relational aspect the database.
https://www.youtube.com/watch?v=da6W7wDh0Dw
Can I use core data on ONE (1) single UIViewController, and if so, how do I call the data and load it into the appropriate fields? Let me know if there's any more info needed.
I'm really trying to understand the Core Data process. What am I missing, or what am I not understanding about the loading process? Any help would be appreciated!
Thanks,
Luke
I have an app that uses a single UIViewController (ONLY ONE VIEW and it's not a UITableViewController).
I only have 2 text boxes and 1 image view.
Assuming there's only one entity returned from the fetch (otherwise how are you going to show them all?), probably something like this:
// WHAT DO I USE HERE IF I'M NOT USING A TABLEVIEW?
//self.tableView.reloadData()
if let fetchResult = frc.fetchedObjects{
if let item = fetchResult.first as? Entity{
textName.text = item.accounttext
descriptionName.text = item.amounttext
imageView.image = UIImage(data: (item.image)! as Data)
}
}
So I was trying out TableViewController and CoreData by making a relatively simple app. All my app does is tell me my credit card balance by adding my transactions up. Also, I'm doing this using two currencies, USD and EGP.
I stored the data permanently before using NSUserDefaults, and it worked. But then I realized that I needed to change to CoreData if I want to make a custom class that stores a lot of details about a transaction (Title, Amount, isEGP...) Also I'm planning on adding date.
Anyway. So I decided to try out CoreData using this RayWenderlich tutorial. I finished everything and when I ran it, it added one element correctly! But when I add another, it crashes with NSRangeException: "1 is out of Range for Bounds[0...0]" I'm modeling my data using a static array of type [NSManagedObject] for all the transactions. The problem seems to have to do with the fact that I'm using a TableViewController not a TableView (i'm guessing) since in the class file here:
class TransactionsTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print (LockScreenViewController.transactions.count)
return LockScreenViewController.transactions.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell (style: UITableViewCellStyle.Default, reuseIdentifier: "Cell");
let transaction = LockScreenViewController.transactions[indexPath.row]
let title = transaction.valueForKey("title") as! String
let amount = transaction.valueForKey("amount") as! Double
let isEGP = transaction.valueForKey("isEGP") as! Bool
cell.textLabel!.text = isEGP ?
(title + ": " + String(amount) + " EGP") :
(title + ": $" + String(amount))
return cell;
}
}
it prints out the count once, and then crashes...instead of 3 or 4 times when there is only 1 element (or no elements). I have no idea how to fix it.
When a button is pressed, I call this function to save the transaction to my array:
func saveTransaction(title: String, amount: Double, isEGP: Bool) {
//1 Managed Context
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
//2
let entity = NSEntityDescription.entityForName("Transaction", inManagedObjectContext: managedContext)
let transaction = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
//3
transaction.setValue(title, forKey: "title");
transaction.setValue(amount, forKey: "amount");
transaction.setValue(isEGP, forKey: "isEGP");
//4
do {
try managedContext.save()
//5
LockScreenViewController.transactions.append(transaction)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
The rest of the code should be fine, but if none of the past code seems to have anything wrong with it, the problem may be because I added a CoreData file after I created the project, instead of checking the "Use Core Data" Button in the beginning. (I fixed the AppDelegate manually, so I don't think that's the issue)
In any case, here is my project file:
http://dropcanvas.com/ogcfo
Thanks!
Core Data + UITableView = NSFetchedResultsController!
You should be using an NSFetchedResultsController. The easiest way to get started is to examine the Xcode template for "Master-Detail" where a table view with a fetched results controller is implemented in the master controller.
Note how the NSFetchedResultsControllerDelegate helps you update the table view dynamically without much effort.
This is by far the most robust solution, plus you get incredible scalability and optimizations.