Percolator
Percolator

Reputation: 523

Overriding existing Core Data with an array of NSManagedObjects

What I want should be simple but for some reason it's not working properly. So, when I rearrange my table view each row is saved in an array friends as a subclass of NSManagedObject in the same order as in the table view(from top to bottom). If the friends view shows two rows, friend1 and friend2, friends[0] is friend1 and friends[1] is friend2.

Whenever the table order has changed, I want the data to be stored in core data in the same order as in the friends array.

Is there any simple way to replace all core data with my friends array?

Upvotes: 0

Views: 60

Answers (3)

Jody Hagins
Jody Hagins

Reputation: 28409

If you want to manage an order that has nothing to do with sorted attributes, you can make the relationship "ordered" which will result in the relationship being NSOrderedSet.

You can then manipulate the ordering however you desire, and when you enumerate the collection, it will be in the order you stipulate.

Note, however, that there are some restrictions on NSOrderedSet which are stipulated in the Core Data documentation.

Upvotes: 1

reviver
reviver

Reputation: 168

Objects in core data has no order, just like Set.

If you have an entity with to-many relationship with friends, then you can use NSOrderedSet to keep order.

Or, you can set up a property to keep order index as a sort condition when you fetch objects from core data.

Upvotes: 0

vadian
vadian

Reputation: 285092

The default collection type in Core Data is NSSet which is an unordered type.

  • If you want a standard order like alphabetical, provide an NSSortDescriptor when fetching the data
  • If you want a custom order add an index field in the data model and assign indexes to the items accordingly. When changing the table view order update the indexes and later fetch the data sorted by index.

Upvotes: 2

Related Questions