I store data with coreData, but still the objects remain nil
I am using master data to store information for objects, but when I try to use it, the program crashes and says, "Fatal error: unexpectedly found nil while unpacking an optional value"
This is the data I am generating
func generateTestData()
{
let item = Item(context: context)
item.title = "New Iphone 7s"
item.price = 2000
item.details = "I wish it something that is worth to apple , unline Iphone 7"
let item2 = Item(context: context)
item2.title = "Beach House in France"
item2.price = 3000000
item2.details = "I will live there for the rest of my Life , untill then i don't have a life"
}
this is a sampling function
func attemptFetch()
{
let fetchRequest :NSFetchRequest<Item> = Item.fetchRequest()
let datasort = NSSortDescriptor(key: "created", ascending: false)
fetchRequest.sortDescriptors = [datasort]
let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
self.controller = controller
do{
try controller.performFetch()
}
catch{
let error = error as NSError
print(error.debugDescription)
}
}
the crash happens here when i try to update my view
func configureCell(cell : objectItemCell , indexPath :IndexPath)
{
let item = controller.object(at:indexPath)
cell.configureCell(item: item)
}
UITableViewCell Class
func configureCell(item : Item)
{
self.title.text = item.title
self.price.text = "$\(item.price)"
self.details.text = item.details
}
source to share
Before fetching data from an element, please save the context. In your script, in generateTestData (), do context.save (), maybe your application crashes because you are not saving data and trying to retrieve, which returns nil.
func generateTestData()
{
let item = Item(context: context)
item.title = "New Iphone 7s"
item.price = 2000
item.details = "I wish it something that is worth to apple , unline Iphone 7"
let item2 = Item(context: context)
item2.title = "Beach House in France"
item2.price = 3000000
item2.details = "I will live there for the rest of my Life , untill then i don't have a life"
saveContext() // save data that you initialised
}
// MARK: - Core Data Saving support
func saveContext () {
if #available(iOS 10.0, *) {
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)")
}
}
} else {
// Fallback on earlier versions
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()
}
}
}
}
source to share
In terms of basic data in iOS, when you use the generateTestData () function, the data is generated in NSManagedObjectContext. Just because it's in NSManagedObjectContext doesn't mean it will be stored in SQLite.
To save the data in SQLite (required to save, so you don't have to run generateTestData () every time) use
ad.saveContext()
The saveContext () file is like a commit statement in terms of db. To use the above, declare the following in your AppDeligate.swift outside of the class definition so you can access your context in your controllers.
let ad = UIApplication.shared.delegate as! AppDelegate
let contextAP = ad.persistentContainer.viewContext
NOTE. Doing the above saves the data in SQLite, but running generateTestData () at the same time with the same data using saveContext () will create duplicate records in your SQLite.
Image link: https://www.objc.io/images/issue-4/stack-simple-9af1e89d.png
source to share
I think you forgot to create managedObjectContext in the class where you are trying to get your objects / entities
import CoreData
sampleClass: UITableViewController, UITableViewDataSource ... {
var context: NSManagedObjectContext!
override func viewDidLoad() {
super.viewDidLoad()
let appDelegate = UIApplication.shared.delegate as! UIAppDelegate
context = appDelegate.persistentContainer.viewContext
}
funcAttemptFetch() {
// your code
}
hope this helps you
source to share