0

I am moving rows inside a collectionView, I have no sorting on the data so all new cells are inserted at the end of the table, but when I move a cell I want to specifically select the index where it should be moved inside the entity in core data, this way the collectionView will automatically display the moved cell at the updated row without any additional work, is that possible without creating an additional ID attribute?

The entity is named List and it has two attributes, task_name and task_bool.

  var myData:[NSManagedObject] = []      

  let appDelegate =
        UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext!
        let newItem: AnyObject =  NSEntityDescription.insertNewObjectForEntityForName("List",
            inManagedObjectContext:
            managedContext)
        var deleltedRow = myArray[indexPath.row].valueForKey("task_name") as! String
        myData.removeAtIndex(indexPath.row)
        newItem.setValue(deleltedRow, forKey: "task_name"); // how would I add atIndex: here??
        newItem.setValue(true, forKey: "task_bool"); // how would I add atIndex: here??
Victor --------
  • 512
  • 1
  • 11
  • 29

2 Answers2

3

Core data maintains an unordered collection of objects. If you want to display those objects in a specific order then you need to sort them, either while fetching or afterwards. That means that the objects need to have some information inside them on which you can sort. You don't have anything currently so you'd need to add an order type attribute and maintain it with appropriate values.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Thanks guys, I'll try to avoid sorting all together in this case by changing the attributes to task_completed and task_uncompleted – Victor -------- Jul 01 '15 at 07:44
1

Core Data does not store objects in an ordered manner. If you want to display these objects in a specific order, you will need a property/attribute in your entity which lets you order the list the way you want.

It might be a good idea to add a order attribute to the entity and use that for the ordering.

lostInTransit
  • 70,519
  • 61
  • 198
  • 274