2

Right now I have a JSON file that is around 30 MBs, the JSON file is a dictionary in the format:

{"chinesePhrase":[
{"traditional": "21三體綜合症", "simplified": "21三体综合症","pinyinRead": "èr shí yī sān tǐ zōng hé zhèng", "pinyinType": "er4 shi2 yi1 san1 ti3 zong1 he2 zheng4", "definition": ["trisomy","Downs syndrome"]},...
]}

Each entry is then put into a table cell, and there is a search bar for users to search which word they want and then when selected it is saved into CoreData.

My question is:
Every time I open the app it has to re-parse the JSON file to populate the table cells; is there a better way to do this so that the JSON file wouldn't have to be re-parsed every time the view controller that has all the dictionary entries is opened?

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
YellowPillow
  • 4,100
  • 6
  • 31
  • 57
  • "If you can recreate those values at runtime (by downloading from the Internet, by doing calculations, whatever) then NSCache may suit your needs. If the data cannot be recreated (e.g. it's user input, it is time-sensitive, etc.) then you should not store it in an NSCache because it will be destroyed there" By Jonathan in this Answer:- http://stackoverflow.com/a/5756162/2714702 – Vizllx Jul 15 '15 at 08:00
  • 2
    Why not just parse it once and store into `CoreData`, you can then achieve things like lazy loading data for your `TableView` as well as still allow users to search on the `CoreData` data? – sbarow Jul 15 '15 at 08:12

1 Answers1

3

You could try this. In the appdelegate "applicationDidBecomeActive" section. Check if this is the initial launch of the app. If so, parse the JSON data.

Then if the JSON parse was successful set the NSUserDefault to indicate this and store the data in CoreData. Or what ever you need to do.

 func applicationDidBecomeActive(application: UIApplication) {

    let firstLaunch = NSUserDefaults.standardUserDefaults().boolForKey("FirstLaunch")
    if firstLaunch  {
        println("Not first launch.")

    }
    else {

       //Parse the JSON file here.

          //if success set NSUserDefault
             NSUserDefaults.standardUserDefaults().setBool(true, forKey: "FirstLaunch")

         //if success, store data in CoreData.

     }
  }

If you want to update the data each time the app is launched. Lets say you added/edited data and wanted user to get this new updated data during next launch. You would have to remove the NSUserDefault settings and firstLaunch logic. And just parse the JSON file and store in CoreData.

Then when ever you needed to display data in your TableView you could easily just fetch it from CoreData.

the_pantless_coder
  • 2,297
  • 1
  • 17
  • 30
  • How did you know where you get this information? I know it's from the swift documentation but how did you know that this function existed? I'm having a really hard time trying to learn Swift myself and I was wondering what is the best way? Should I just study the documentation? – YellowPillow Jul 15 '15 at 18:36
  • I found it very helpful to read through the apple docs a few times. I tend to read it on the bus/tram/crapper. Waiting for friends etc..Basically whenever I have a few minutes. I open up my Iphone and read a section/chapter or two. You will find as you go on... delegates are your friend. :) Do a few online tutorials on youtube it will help you alot. They do not even have to be for an app you are interested in. But each one will show you something new. Goodluck and happy coding! – the_pantless_coder Jul 15 '15 at 20:16
  • 1
    Which one are you reading could you provide me with the link? I don't think I'm reading the correct one as it is about variables and classes etc. – YellowPillow Jul 15 '15 at 20:23
  • This is a good link to to get you started. https://developer.apple.com/library/ios/navigation/ Dont be put off by the amount of stuff there. Like I said this was just in my "extra" time. Instead of staring off out the window of the tram/bus I would read it. When I was learning about CoreData. I would do a few tutorials on youtube. Then after each I would read the section related to the tutorial. Or if I did not understand something in tutorial, I would pause and read up on it until it made sense. Then continue. Of course each person has their own ways of learning. This is what works for me. – the_pantless_coder Jul 15 '15 at 20:45
  • I would start with "Getting Started" Then just pick frameworks that interest you or ones that you are having problems with. When I was starting I focused on making basic silly games using spritekit. Was a fun way to get my feet wet. – the_pantless_coder Jul 15 '15 at 23:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83457/discussion-between-user3529361-and-the-pantless-coder). – YellowPillow Jul 16 '15 at 16:22