I have to store about 10k text lines in an Array. Each line is stored as a separate encrypted entry. When the app runs I only need to access a small number and decrypt them – depending on user input. I thought of some kind of lazy evaluation but don’t know how to do it in this case.
This is how I build up my array: [allElements addObject: @"wdhkasuqqbuqwz" ] The string is encrypted. Accessing is like txt = [[allElements objectAtIndex:n] decrypt]
The problem currently is that this uses lots of memory from the very start – most of the items I don’t need anyway, just don’t know which ones ;). Also I am hesitant to store the text externally eg in a textfile, since this would make it easier to access it.
Is there a way to minimize memory usage in such a case?
ps initialization is very fast, so no issue here
So it’s quite a big array, although not really big enough to be triggering any huge memory warnings (unless my maths has gone horribly wrong, I reckon your array of 10,000 40-character strings is about 0.76 MB. Perhaps there are other things going on in your app causing these warnings – are you loading any large images or many assets?
What I’m a little confused about it how you’re currently storing these elements before you initalise the array. Because you say you don’t want to store the text externally in a text file, but you must be holding them in some kind of file before initialising your array, unless of course your values are generated on the fly.
If you’ve encrypted correctly, you shouldn’t need to care whether your values are stored in plain-sight or not. Hopefully you’re using an established standard and not rolling your own encryption, so really I think worrying about users getting hold of the file is a moot point. After all, the whole point of encryption is being able to hide data in plain sight.
I would recommend, as a couple of your commenters already have, is that you should just use some form of database storage. Core Data was made for this purpose – handling large amounts of data with minimal memory impact. But again, I’m not sure how that array alone could trigger a memory warning, so I suspect there’s other stuff going on in your app that’s eating up your memory.