2

I am trying to build an app for my website. I was under the impression that all I had to do was get and put json from the server to my phone. But then today I came across Core Data ( I am new to iOS).

So my question is, that to make a faster iOS app, is it a normal practice to fetch json data and save it as core data? Would apps like Facebook, Twitter follow this approach? or is just fetching and parsing json is a normal practice, and core data is not needed.

I am sorry if the question is dumb.

Jonathan
  • 2,728
  • 10
  • 43
  • 73

2 Answers2

2

It is normal to retrieve data from a server (XML or JSON) and keep it in memory, if the memory foot print is reasonable. If you are talking about hundreds upon thousands of rows from a database, then persistent storage, with a dedicated data model(s) is probably the best choice; you can read it when needed.

If your needs are such that a complex data model is needed, one-to-many and/or many-to-many relationships, then consider Core Data (or SQLite directly).

You define your needs first, then try to define the data model that fits your needs (custom objects or maybe just a few instances of NSDictionary), then decide how that data needs to persist and how you plan on interacting with that data.

A few starting points:

Good luck.

Mike D
  • 4,938
  • 6
  • 43
  • 99
  • Mike, I do not anticipate having to save more than 100 objects per page. its just going to be simple posts with test, video and images on them. and users can comment on the post, etc. just the standard stuff. so if I understand right for such trivial operations I dont need core data right away? most of my operations would be the operations performed on applications like facebook and twitter – Jonathan Mar 07 '13 at 04:38
  • @Jonathan There you go then. Once you get a prototype built, you will have a better idea. – Mike D Mar 07 '13 at 04:42
  • I think thats the right approach. I will stick with Synchronous and Asynchronous NSURL connections for now, handle data like that and then if I feel the need to switch to Core Data at some point, I would implement that later. :) Thank you. I do have one more question though, what is the best way to handle logins for users? how does facebook login users once and then the user doesnt have to login again? do they use session cookies? but even cookies expire in two weeks. I am very confused about that – Jonathan Mar 07 '13 at 04:46
  • @Jonathan I am not an expert on the Facebook API, but my guess is OAuth, or something similar. But I think that is stand alone question. – Mike D Mar 07 '13 at 04:53
  • Mike, I dont need to make users login using facebook. I am just confused that how should login be implemented. but yes you are right, its a stand alone question, I will do some more research, if I cannot find an answer I will as this as a question on stackoverflow. – Jonathan Mar 07 '13 at 04:55
1

I remember facing this anomaly not so long ago.

As already pointed out in some of the comments, it depends on your needs.

Not all data are eligible to be saved in core data after retrieving it from the web side. You might have integrity issues with that. To do the checks for large chunks of data might have even severe overheads. But if you feel that certain data are not likely to change very often then you can employ this technique for some portions.

If you decide to stick with Request/Fetch data, be sure you process the requests using NSOperation, GCD or NSThread, in order to avoid UI freezes.

Although, they are used for same purpose, they all have advantages and disadvantage, plz check out this topic on NSOperation vs Grand Central Dispatch

I hope this helps.

Community
  • 1
  • 1
Kishor Kundan
  • 3,135
  • 1
  • 23
  • 30
  • Kishor, I do anticipate users making posts quickly. I am making an application like facebook and twitter. its a simple posting app, where users can post video, pictures and text. – Jonathan Mar 07 '13 at 04:39
  • 1
    Well in that case, Core data will be flooded with all those data in no time and I guess not all of them will relevant for every session. I think u sud employ caching techniques on the web side. Also, if u have noticed facebook only retrieves latest feeds like a feed/msg from a day back or at most a week and fetches additional when ppl scroll there or explicitly issue an request. You can employ such things to keep ur request lifecycle small and fast. – Kishor Kundan Mar 07 '13 at 04:45
  • 1
    exactly! thats exactly what I need. Fetch maybe recent 20 posts and fetch more when the user scrolls down. – Jonathan Mar 07 '13 at 04:47
  • also Kishor, I asked Mike this question as well, what is the best way to handle logins for users? how does facebook login users once and then the user doesnt have to login again? do they use session cookies? but even cookies expire in two weeks. I am very confused about that – Jonathan Mar 07 '13 at 04:49
  • I use keychain to store Auth credentials locally, once the users have logged in. If my app does not have active authentication object then I make a request using the Auth credentials from the keychain. The server then verifies the request, if it is verified successfully then I create the Auth Object for the App, if it doesn't I present the Login View (this also applies in case Auth credentials are missing in keychain) – Kishor Kundan Mar 07 '13 at 04:54
  • that sounds like a plan, would you happen to know some tutorial that maybe I can look into for this? – Jonathan Mar 07 '13 at 04:56
  • I don't quite remember what I had done to learn this. I just googled around and this came up nice. http://iosdevelopertips.com/core-services/using-keychain-to-store-username-and-password.html – Kishor Kundan Mar 07 '13 at 05:08