3lvis/DATASource

Cells placed into wrong section

syky27 opened this issue · 5 comments

Hi, I have rather big problem, when I use DATASource to place fetched objects from Core Data to UITableView with sections, some of the objects will get placed to wrong section.

I am using following code:

    lazy var dataSource: DATASource = {
        let request: NSFetchRequest = NSFetchRequest(entityName: "TypicalFindingDescription")
        request.sortDescriptors = [NSSortDescriptor(key: "materialRemoteID", ascending: true)]

        let dataSource = DATASource(tableView: self.tableView, cellIdentifier: TypicalDescriptionTableViewCell.Identifier, fetchRequest: request, mainContext: self.dataStack.mainContext, sectionName: "materialName", configuration: {
            cell, item, indexPath in
            if let name = item.valueForKey("name") as? String, let materialName = item.valueForKey("materialName") as? String, let materialRemoteID = item.valueForKey("materialRemoteID") as? Int {
                if let cell = cell as? TypicalDescriptionTableViewCell {
                    cell.textLabel!.text = name
                    cell.textLabel!.lineBreakMode = .ByWordWrapping
                    cell.textLabel!.numberOfLines = 0
                }
            }
        })

        return dataSource
    }()

I triple checked that the data are correctly stored in Core Data.

This actually occurs on two different Entities, according to my testers, but I haven't checked the other one.

Thank you for any help.

It is happening in both cases here is the other piece of code:

lazy var dataSource: DATASource = {
        let request: NSFetchRequest = NSFetchRequest(entityName: "Report")
        request.sortDescriptors = [NSSortDescriptor(key: "modifyDate", ascending: false)]


        let dataSource = DATASource(tableView: self.tableView, cellIdentifier: ReportTableViewCell.Identifier, fetchRequest: request, mainContext: self.dataStack.mainContext, sectionName:  "equipment.equipmentType.company.companyName", configuration: { cell, item, indexPath in

            if let cell = cell as? ReportTableViewCell {
                let report = item as! Report
                if let imagePath = report.equipment!.path {
                    cell.thumbnail.image = PicManager.instance.getThumbnail(imagePath, type: K.file.equipmentThumbnails)
                }

                if let name = item.valueForKey("name") as? String {
                    cell.name.text = name
                }

                if let desc = item.valueForKey("reportDescription") as? String {
                    cell.descriptionLabel.text = desc
                }

                if let cwaId = item.valueForKey("cwaId") as? String {
                    cell.cwaId.text = "CWA# " + cwaId
                }

                if let final = item.valueForKey("final") as? Bool {

                }

                if let modifyDate = item.valueForKey("modifyDate") as? NSDate {
                    cell.modifiedDate.text = "Modified " + NSDate.stringForInspectionCell(modifyDate)
                }

                if let equipment = report.equipment {
                    if let equipmentName = equipment.name {
                        cell.equipment.text = equipmentName
                        if let equipmentType = equipment.equipmentType {
                            if let equipmentTypeName = equipmentType.name {
                                if equipmentTypeName != "" {
                                    cell.equipment.text = equipmentTypeName + " - " + equipmentName
                                } else {
                                    cell.equipment.text = equipmentName
                                }

                            }
                        }
                    }
                }
            }
        })
        dataSource.delegate = self
        return dataSource
    }()

Here is a data source on which you can test it.

http://haste.fit.cvut.cz/dokodig.json

So I found out this might be my mistake having different sortDescriptor and sectionName.

I changed it to same one and it works but it should work with different ones because they aways point correctly to to the same object.

Anyway I rewritten it to NsFetchResultController and it behaves identical. So it is not your mistake.

If you could comment on this and give me insight why is this behaving as it is it would be great.

3lvis commented

Hi @syky27, I have been in vacation mode. Will try to reply later this week. Sorry for not being so responsive.

3lvis commented

@syky27 Hi Tomas, this seems as you mentioned a NSFetchedResultsController issue, where you have to add the sortDescriptor if you are using sectioned table views, I remember getting an error from UIKit before about this but looks like they removed this error.

For example if you are grouping by firstName you have to add firstName as the first sortDescriptor. I'll add this to the README.