I'm developing an app that shows a Random Quote when you press a button, and anyone knows if is possible to put all my quotes in a CoreData, and when the User press the button, it'll give another quote. How can I do this?
I'm developing an app that shows a Random Quote when you press a button, and anyone knows if is possible to put all my quotes in a CoreData, and when the User press the button, it'll give another quote. How can I do this?
Thanks,
Lucas
when the app loads you could do a check to see if the data is there and if it's not, then load it into core data. One way to do this would be to create a json file that has all the quotes you want on it and read from that file for your initial load. This way, if you ever want to add any you just need to update that file rather then hardcoding it in place.
If you have a lot to load, maybe. But only on the very first time the app is run. I'm not sure if 1000 would be a problem though. I have never load tested core data. Should be easy enough to find out though by experimenting.
If you do it this way it probably would make sense to create all your objects and save them at the same time. Don't forget to clean 'em up when you are done too.
how do i think it's the best way? It's the only way I know. Best? If someone puts up another solution then I could compare them I guess. But I think it's the only way to add initial data.
And how do you think I can get a random quote from the Json File?
Store the quotes using core data, when you want to display a random quote then fetch all the quotes from core data and they will be stored in an array. Get a random number from 0 to the array count and then access the quote at that array index.