错误-使用UITableView时,核心数据只能访问对象一次 [英] Faults - core data can only acces object once when working with UITableView

查看:82
本文介绍了错误-使用UITableView时,核心数据只能访问对象一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是一个奇怪的错误,我根本无法弄清楚.我从核心数据实体获取费用.然后,我将这些结果按日期排序.我使用这种排序的结构来获取tableView中正确的节和行的正确数据.这段代码都能正常工作,并且TableView出口中的单元格已正确填写到cellForRowAtIndexPath中.但是,当一个单元格离开tableView(=排队)并随后又重新加载回(= Dequeued)时,我将无法再访问费用及其关系... 让我进一步解释:这里是核心数据结构

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

然后,当单元加载时,我想访问expenseHasCat关系.第一次加载表时,一切正常.

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
    }

但是由于某些原因如果单元格退出tableView并在以后加载回去,我将无法再支付费用.它说在展开一个可选文件时为零.我不知道为什么会这样.费用字典完全没有改变.我只是不知道为什么它会在第一次而不是第二次起作用.

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?

更新! -故障一定是基本问题...

我刚刚注意到,在self.expensesDictionary [pos] .1上 Expenses对象的打印无法触发错误,或者值现在为零.它加载,如果无法加载则第二.有人知道如何从故障中提取数据吗?

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>)

我尚未找到解决方案

此处的线程堆栈: https://www.dropbox.com/s/zvpvlcsqjpqcxns/Screenshot%202015-11-16%2015.42.09.png?dl=0

推荐答案

如果提取请求导致的对象是错误的,请使用

If objects resulting from fetch request are faults use

yourFetchRequest.returnsObjectsAsFaults = false; 

以禁用它们.

这篇关于错误-使用UITableView时,核心数据只能访问对象一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆