0

I am writing a custom keyboard for an Asian language and I have a word database with over half a million words. I use Realm for now and use it to give word suggestions. When users type the first few letters keyboard will search the DB and provide words based on priority values given to each word. But this seems inefficient compared to other keyboards in the App Store, I can't find any concrete way or idea on this issue. Anyone can point me in the direction to increase the efficiency of word searching with a custom iOS keyboard.

I haven't tried CoreData but generally, the realm is considered faster than CoreData.

aheze
  • 24,434
  • 8
  • 68
  • 125
RJE
  • 781
  • 6
  • 14
  • *But this seems inefficient* why? What kind of difficulties are you having? The issue could also be inefficient or malformed code so can you include that? e.g. sometimes we see realm objects cast to arrays which makes them less efficient but without knowing what you're doing it was purely be guesswork for us. – Jay Aug 04 '21 at 22:00
  • @Jay Ok main issue is like that. The keyboard doesn't really lag, the user can continue typing. But suggestion bar seems lagging coz I needs to read database again and again and it won't display suggestion as smoothly as an iOS stock keyboard or some other custom keyboards. Actually, my keyboard has both Unicode and English layouts. for English, I use the iOS internal dictionary with `UITextChecker` and it is lightning fast to display suggestions on the same custom UI suggestion bar. The issue is with my own dictionary/database. – RJE Aug 05 '21 at 03:23
  • I'm currently just doing a simple query with `BEGINSWITH` and sorted by `priority` on the realm table. The final result will be cast to an array before returning to display on the keyboard. – RJE Aug 05 '21 at 03:33
  • *The final result will be cast to an array* which is probably the issue because you could be returning millions of results. We have a 2Gb database we are pulling from and it's lightning fast as we are letting Realm do the heavy lifting - Realm objects are lazily loaded so even large amounts of stored data results in a smaller memory footprint - however, keeping Realm objects in Realm collections is the key. Again though - without knowing what/how your code works we are just guessing. – Jay Aug 05 '21 at 17:41
  • I think you are correct, I removed the array and it seems better. I also break the words table into multiple smaller tables (for each starting letter) and now it is better and I am happy with the final result. Thanks for the help – RJE Aug 06 '21 at 05:24
  • Glad I could help! – Jay Aug 06 '21 at 16:51

1 Answers1

0

First, type of storage: maybe consider using plist files or .text files wouldn't be bad. and saving words in a sorted way in ASCII mode would be great.

Second: you need an algorithm to break into a group of words so fast. you can do this by saving the ASCII code.

Here is an example of a binary search algorithm :

[GIF of binary search vs sequential search]

Please search around about different algorithms.

aheze
  • 24,434
  • 8
  • 68
  • 125
Narjes
  • 26
  • 1
  • 9
  • I would not suggest using plist or .text files compared to an indexed database like realm. That's going to be way slower - even with split half or binary searches. It's not a bad answer, but for larger datasets, it's not a good solution. – Jay Aug 04 '21 at 22:01
  • I am thinking of breaking the database table into multiple tables, for the first letter of the word. Technically I don't know it's any use, but I'm willing to try anything at the moment. – RJE Aug 05 '21 at 03:28
  • I break my table into multiple smaller tables. Also removed some unessesory castings and now it seems much better. – RJE Aug 06 '21 at 05:26
  • @jay yes I agree with you, I assumed RJE has limited words and it's not going to become bigger. – Narjes Aug 06 '21 at 08:55