I’m looking for a memory efficient way to take only one row from a fetch result set. This must be random.
I thought using [context countForFetchRequest:fetch error:nil]; and get an int random value between 0 and that and offset + limit the fetch to 1 result. But I can’t find whether or not it doesn’t allocate memory for each item it counts.
Is “count” a lightweight operation? Or does it need to instantiate objects in the context before being able to count them?
The documentation is somewhat unclear, but it includes the phrase “number of objects a given fetch request would have returned.” Furthermore, Core Data tends to make things like
countvery lightweight – entity instances, for example, allow you to callcountto find out the number of objects on the end of a to-many relationship without instantiating all those objects or firing that fault. I’d say go for it, but profile it yourself – don’t optimize prematurely!