NSFetchRequest without sort descriptors
Asked Answered
S

3

6

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).

Selfstarter answered 3/8, 2010 at 14:46 Comment(0)
D
7

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.

Drawbar answered 3/8, 2010 at 14:57 Comment(2)
Thanks, i guess that's my only option.Selfstarter
A bit of an update on this: as of iOS5 it's possible to make ordered relationships. HOWEVER, NSFetchedResultsController still requires that a NSFetchRequest specify a sort descriptor, even though the field is declared as sorted. I have not found a solution to this yet.Citrin
I
6

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.

Impecunious answered 4/8, 2010 at 12:25 Comment(4)
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.Selfstarter
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.Impecunious
Please ignore my last comment. That was intended for another post.Impecunious
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.Impecunious
W
1
[[NSSortDescriptor alloc] initWithKey:@"" ascending:NO];

In my project, I use NSFetchRequest to fetch a list, and all objects in the list have a property which have the same value. so we use that property's name of the "initWithKey:" . It work well.

Win answered 24/8, 2016 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.