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:
-
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.
-
fetch random words using offsets on the index i.e. 0 – 20 = 2 letter words, 21 – 972 = 3 letter words
-
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.
I think I have found my own answer through some testing.
method (1) is obviously the simple way but far too resource intensive to fetch and store massive amount of data.
method (3) is just silly.
so I opted for method (2) with some newly acquired Core Data skills.
The method is as follows:
Although not completely random, the method above is quick, and returns 10 sequential values, from a random offset, and stores it to an NSMutableArray.
Update: The actual code and detailed explanation can be found on my tutorial page: http://prasannaellanti.com/tutorials/
Any other suggestions welcome.
Pras.