I've been struggling with this all afternoon. I can do this in Obj-C in my sleep but I can't seem to grasp what I am suppose to do in Swift. I can get an array of entities back (StoredTopic) but I keep getting errors trying to access them.
Here's the fetch code:
func fetchCoreData(_ entityName:String) -> [NSManagedObject] {
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return []
}
let managedContext =
appDelegate.persistentContainer.viewContext
let fetchRequest =
NSFetchRequest<NSManagedObject>(entityName: entityName)
do {
let arrData = try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
return []
}
Here's the log of arrData
[<StoredTopic: 0x6040000833e0> (entity: StoredTopic; id: 0xd000000000040000 <x-coredata://C27694CF-4EB0-4F28-8D7D-4DFFAA1051A4/StoredTopic/p1> ; data: <fault>), <StoredTopic: 0x604000085960> (entity: StoredTopic; id: 0xd000000000080000 <x-coredata://C27694CF-4EB0-4F28-8D7D-4DFFAA1051A4/StoredTopic/p2> ; data: <fault>), <StoredTopic: 0x604000083020> (entity: StoredTopic; id: 0xd0000000000c0000 <x-coredata://C27694CF-4EB0-4F28-8D7D-4DFFAA1051A4/StoredTopic/p3> ; data: <fault>), <StoredTopic: 0x604000088de0> (entity: StoredTopic; id: 0xd000000000100000 <x-coredata://C27694CF-4EB0-4F28-8D7D-4DFFAA1051A4/StoredTopic/p4> ; data: <fault>), <StoredTopic: 0x60000008c580> (entity: StoredTopic; id: 0xd000000000140000 <x-coredata://C27694CF-4EB0-4F28-8D7D-4DFFAA1051A4/StoredTopic/p5> ; data: {
createDt = "2017-11-02 19:28:00 +0000";
segObj = 0;
segTopic = 0;
title = Test;
topicID = "4A55582B-3084-4ECA-8F61-C1193A85B45B";
"topic_desc" = 1234567890;
updateDt = "2017-11-02 19:28:00 +0000";
So if I try to do something like:
arrData[0].topicID I get "Value of type 'NSManagedObject' has no member 'topicID'"
If I try to do like some other SO post suggested by adding this line:
var arrStoredTopic = [StoredTopic]()
arrStoredTopic = try managedContext.fetch(fetchRequest)
I get "Cannot assign value of type '[NSManagedObject]' to type '[StoredTopic]'"
I then tried to follow this tutorial, though it was a little outdated with some of the calls and changed my code to follow it:
func fetchCoreData(_ entityName:String) -> [NSManagedObject] {
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return []
}
let managedContext = appDelegate.persistentContainer.viewContext
var fetchRequest = NSFetchRequest<NSFetchRequestResult>()
let entityDescription = NSEntityDescription.entity(forEntityName: entityName, in: managedContext)
fetchRequest.entity = entityDescription
do {
//let arrData = try managedContext.fetch(fetchRequest)
let results = try managedContext.fetch(fetchRequest)
if results.count > 0 {
let storedData = results[0] as! NSManagedObject
}
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
return []
}
Still not able to access the StoredTopic properties.
So how do I fetch managedObjects from Core Data so I can work with them? I'm obviously confused.
Thanks