Core Data Fetching

Hi


Im strugeling in fetching data from core data first it was the egenric thing Im not sure if its solved or not, aftering getting an array of managed objects Im struggeling feeding its data to table view cell, I think it suppose to be a simple task ?

--

Kindest Regards

h ttps://www.dropbox.com/s/vb3cet085v05tvi/Core%20Data%20Simple.zip?dl=0

Answered by Claude31 in 414709022

Cannot launch the app. Crashes because "normalCell" cannot be dequeued in ViewController (cellForRowAt).


Just had to add to cell in IB.

Now, I get one cell with Iraq / Asia.

What do you expect here ?


What is the request that does not work ?

Is it this one:

   override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
       
        self.context = testData()
       
        // let students: NSFetchRequest = Student.fetchRequest
        let students = NSFetchRequest(entityName: "Student")
       
        try! results = context!.execute(students) as? [NSManagedObject]
        // results = context.executeFetchRequest(students) as? [NSManagedObject]
    }



results is nil, because execute does not return the correct type.


Use instead:

        try! results = context!.fetch(students) as? [NSManagedObject]



Read this for (some) details:

https://stackoverflow.com/questions/53971228/could-not-cast-value-of-type-nsasynchronousfetchresult-0x103e13388-to-nsarr

Accepted Answer

Cannot launch the app. Crashes because "normalCell" cannot be dequeued in ViewController (cellForRowAt).


Just had to add to cell in IB.

Now, I get one cell with Iraq / Asia.

What do you expect here ?


What is the request that does not work ?

Is it this one:

   override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
       
        self.context = testData()
       
        // let students: NSFetchRequest = Student.fetchRequest
        let students = NSFetchRequest(entityName: "Student")
       
        try! results = context!.execute(students) as? [NSManagedObject]
        // results = context.executeFetchRequest(students) as? [NSManagedObject]
    }



results is nil, because execute does not return the correct type.


Use instead:

        try! results = context!.fetch(students) as? [NSManagedObject]



Read this for (some) details:

https://stackoverflow.com/questions/53971228/could-not-cast-value-of-type-nsasynchronousfetchresult-0x103e13388-to-nsarr

The line that was not working was first line below, I get an error "Generic parameter 'ResultType' could not be inferred" then I managed to solve it with second line, but I dont reaaaly understand the error and the second line .. 🙂


First line

let students = NSFetchRequest(entityName: "Student")


Second line

let students = NSFetchRequest<NSFetchRequestResult>(entityName: "Student")

See attached project with error below its not recognizing the Capital Class ! or the Capital entity ! "CoreData: warning: Unable to load class named 'Capital' for entity 'Capital'. Class not found, using default NSManagedObject instead."


h ttps://www.dropbox.com/s/dkkfnyyhg6riyas/Core.zip?dl=0

It is not that the first line is not working. It is a warning, not an error.


Simply, cannot find the class corresponding to Student, so uses a generic class.


That may be normal the way you build the entity.


The real problem was the other line

context!.execute(students)


to be replaced by context!.fetch(students)


Did you try ?

See theres confusion going here when you say it didnt find the Student class true but what Im expecting that it will use the "Syudent" entity in the managed obect model right ? we are not using a class here, I saw something like this statment changed since swift 3 so the line I mentioned below solved it, yes I used the line you mentioned it helped solve the error in that line.

Core Data Fetching
 
 
Q