Reputation: 3093
I have some records in core data, its contain name,fulsome,email,image etc.. I was showing this data in collection view, In there I was providing drag-drop/move cell functionality. Now How to Update reordered/rearrange data in core data? please help.
Upvotes: 0
Views: 716
Reputation: 1848
CoreData does not have any ordering.
What you have to do is to order the data you fetch. For example if you use a NSFetchRequest
you can add an array of NSSortDescriptors
which specify the sort ordering of the fetched data
So, for example:
NSFetchRequest *request = ....
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];
EDIT: after the comment:
If the ordering must be persisted to the CoreData store, the only way I see is to add in your model an integer attribute. By default you can leave it at -1
, i.e., no order.
When you perform the drag and drop in the UI you can then save the index to the attribute and persist it in the store.
Your fetch request can become something like
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"order" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];
In this way in case of equal ordering (-1) the name will be taken into account, otherwise the index have priority.
Upvotes: 3