2 Votes Vote

Core Data - How to carry out complex random fetch

Posted by topdog 26 days ago Questions| random fetch words All

This is more of a general design query with Core Data and how to design it so that it is efficient.

My app is a word game, with an sqlite3 database which holds a dictionary of words ranging from 2 - 28 letter words. The database has columns for the words and the size of the word.

The result I want to get (and the query I would like to design) is to fetch 10 random words of each size i.e. 10 random 2 letter words, 10 random 3 letter words and so on.

Additionally, I am trying to do this fetch up front in the AppDelegate as my application is loading.

I thought about doing this a few ways, but really need your opinions:

  1. fetch all letters of a certain size, and then get random records within the results. This means multiple fetches for each word length, and storing a lot of data.

  2. fetch random words using offsets on the index i.e. 0 - 20 = 2 letter words, 21 - 972 = 3 letter words

  3. fetch a random say 500 records based on a random index, and hope this contains at least 10 words of each length.

I am not sure how to design the query efficiently. I am trying to stick to the rule of a single fetch, and sort data after as much as possible.

Thanks in advance. Pras.

Originally asked by: prazzledazzle on Stack Overflow

Discuss Bury


Who Voted for this Question