Faults - core data can only acces object once when working with UITableView

独自空忆成欢 提交于 2019-12-25 03:34:53

问题


Ok, this is a weird bug I just can't figure out. I fetch expenses from a core data entity. Then I sort those results on the date. I use this sorted structure to get the correct data for the correct section and row in the tableView. This code all works and the cells in the TableView outlets are filled in correctly in cellForRowAtIndexPath. But when a cell goes out of the tableView (=queued) and later gets loaded in back again(=Dequeued), I can't access the expense and its relationship anymore... Let me further explain: Here the core data structures

extension Category {
@NSManaged var name: String?
@NSManaged var catHasExpenses: NSOrderedSet? }


extension Expenses {
@NSManaged var amount: NSNumber
@NSManaged var name: String?
@NSManaged var date: NSDate
@NSManaged var expenseHasCat: Category }

I fetch the data and sort it. This code still works fine, so moving on. When its sorted its of datastructure..

var expensesDictionary = ([(NSDate, Expenses)])() //Array of tuples

Then when the cells load I want to access the expenseHasCat relation. Everything works fine the first time the table loads.

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {   
        let cell = tableView.dequeueReusableCellWithIdentifier("expensesCell", forIndexPath: indexPath) as! CustomExpensesTableViewCell    
        let pos = getPositionInDictionary(indexPath.section, row: indexPath.row)

        // set Category image - imageName = expenseHasCat.name
        var expense = self.expensesDictionary[pos].1
        NSLog("\(expense)") //check printout below
        cell.expenseCategory.image = UIImage(named: (expense.expenseHasCat.name)! as String);
        return cell
    }

But for some reason if the cell goes out of tableView and gets loaded back in later I can't access the expense anymore. It says it is nil while unwrapping an optional. I can't figure out why that would be the case. The expensesDictionary has not been changed at all. I just can't figure out why it would work the first time but not the second time.

Anyone an eye on what happens here?

Update! - fault must be the base problem...

I just noticed that print of the Expenses object at self.expensesDictionary[pos].1 is a not able to fire the fault or the values are somehow nil now... First print is when it loads, second if when it can't load. Anyone an idea how I can extract the data from the fault?

    <x-coredata://9FBBD46A-CD75-4DAB-A796-5BA945927C9A/Expenses/p23> 
; data: {
    amount = 8;
    date = "2015-11-14 21:57:51 +0000";
    expenseHasCat = "0x7ba6a310 <x-coredata://9FBBD46A-CD75-4DAB-A796-5BA945927C9A/Category/p8>";
    name = nil;
})

(entity: Expenses; id: 0x7ba71b00
<x-coredata://9FBBD46A-CD75-4DAB-A796-5BA945927C9A/Expenses/p23> 
; data: <fault>)

I did not yet find a solution

The thread stack here: https://www.dropbox.com/s/zvpvlcsqjpqcxns/Screenshot%202015-11-16%2015.42.09.png?dl=0


回答1:


Literally had this problem yesterday, how are you registering the customTableViewCell's class? If you have a reuse identifier on storyboard then delete the tableView.registerClass() line you may have included in view did load. Check your identity inspector and make sure your class on storyboard matches your custom class. Let me know how you are registering the cell.




回答2:


If objects resulting from fetch request are faults use

yourFetchRequest.returnsObjectsAsFaults = false; 

to disable them.



来源:https://stackoverflow.com/questions/33704370/faults-core-data-can-only-acces-object-once-when-working-with-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!