views:

117

answers:

2

We cannot use NSFetchRequest without providing NSSortDescriptor(s). All i want to do is fetch the results and show them in the order in which they were created. Is there a built-in way to do that?, or will i have to create a new "auto-increment" field? (which goes against everything CoreData stands for, in my opinion).

+3  A: 

Core Data does not guarantee anything about order. You don't have to make an "auto-increment" field; however, you can make an appropriate attribute. Since you obviously care about the date something was created, you should add a dateCreated attribute to your data object. Then, sort by that.

You can easily set that in the managed object's didAwakeFromInsert method.

Jason Coco
Thanks, i guess that's my only option.
Mustafa
+1  A: 

It sounds like you're thinking of Core Data in terms of tables and expect that there should be an automatic ordering just like the row number of table provides. However, Core Data does not use tables, rows or any other fixed, linear data structure.

There is no built-in order for fetches because each fetch request is highly specific to the needs of the view it helps populate. NSFetchRequest requires a sort descriptor because it returns an array and requires a sort descriptor to turn the unordered managed objects within the object graph into an ordered array.

There is no built-in order for managed objects in the object graph in general because each data model has different criteria for structuring the data. E.g for most models, the temporal sequences in which objects are inserted is irrelevant. Why build in a timestamp into every managed object that every Core Data app everywhere must use when it will only be needed in a tiny minority of cases?

If the temporal sequences is an important part of your model's logical relationships i.e. it reflects the real objects, events or conditions that the data model simulates then, as Jason Coco suggested, you should add a timestamp attribute to the entity itself such that the entities objects will model the time of their own creation.

TechZen
Thanks for your valuable input. Actually, i'm not thinking about Core Data in terms of tables, because it's based on sets. But since it does internally store managed object ids, i was hoping to avoid adding any new attributes. I guess i don't have a choice.
Mustafa
I don't think you understand the utility of Core Data. Fetch request return arrays so the sets Core Data uses in relationships does not cause a problem with tables. NSFetchedResultsController was created specifically to make using Core Data with UITableViews easy. Apple intends that Core Data should be the primary data model management system for all Apple platfom apps. As such, Core Data is the quickest and cleanest solution for all but the simplest of data structures. It has a bit of a learning curve but it is well worth the time if going to writing apps for Apple platforms.
TechZen
Please ignore my last comment. That was intended for another post.
TechZen
I am concerned you're not quite getting Core Data which will cause you problems down the road. The more people know about non-object DBs the harder time they have with object-oriented DBs. Why would you even suspect that Core Data has a built-in timestamp or any other ordering? Whats the difference between ordering on creation date and any other ordering? I mean, you can't sort alphabetically without adding a string attribute so why would expect to order on time without a data attribute? ObjectIDs have nothing to do with ordering. By design, they contain random elements that defeat sorting.
TechZen