0

I understand that Core Data is essentially a self-contained local database, but I'm not sure if I should be using it in my app or not. Basically, it would be more for caching purposes if anything, since I retrieve all of my content from a web server database. Regardless, I was wondering if Core Data would be useful is any of these situations:

Scenario #1: I retrieve a list of "items" from the web server and feed them into a table view. This is essentially the first page the user sees. The table can be refreshed to retrieve more results, but existing items likely won't change. Over time this list of items could grow tremendously. Items can be deleted.

Scenario #2: A user has a friends list. This list of friends will stay the same unless he or she adds more friends. I imagine there will be a scenario where a friend deletes their account, in which case the friends list will be altered as well.

Scenario #3: Messages can be attached to items. They can't be edited or deleted, so the only change in state for a list of messages would be if a new message was added. Essentially the same as items, except they can't be deleted.

Ryan Bobrowski
  • 2,641
  • 3
  • 31
  • 52

1 Answers1

1

Actually, for your scenario I would say that you don't need any persistence in your app, but rather fetch your data from the server every time the app starts and just keep it in memory. There are a lot of apps which are doing it this way and this is totally fine behaviour.

However, there are some drawbacks of not using persistence:

  • worse offline experience for your user since they depend on a network connection, so effectively without a connection they can't do anything within your app
  • risk of slow loading

On the plus side we have:

  • using Core Data in your app is a huge implementation overhead (especially if you haven't used it before)
  • after having integrated Core Data, you still have a lot of issues to tackle, first and foremost: data synching between your app and the backend

If you decide to go for persistence, also take a look at alternatives to Core Data like Realm.

Finally, my advice still is to not use Core Data in your situation. However, keep in mind that you can build a version of your app that doesn't use persistence. And then, once you see that your app is well-received and gets more attention, you can still go and add persistence later on.

nburk
  • 22,409
  • 18
  • 87
  • 132
  • Thanks nburk. Question about "keeping the data in memory". Right now when I retrieve items for the table view, the items are purged when I deinit that particular view controller. When you go back to that page via the navigation, it sends the request for items again. Should I be using some collection manager singleton to keep that data in memory, and then check if its empty or not each time? – Ryan Bobrowski Jun 05 '15 at 14:50
  • hmm... this is hard to judge without knowing the app or the navigation it exposes. in general, I wouldn't recommend to do so. but if you feel that it can get really annoying that the data has to be loaded each time the view appears, then it might be a good idea to use a dedicated container to keep some of the data in memory... – nburk Jun 05 '15 at 14:53
  • I mean I was thinking it would be nice to have it persist in that way, and then on each viewWillAppear just send an async check to the db to see if there are any new items, instead of re-retrieving the whole list each time. The only issue is that these items may have a delete feature later on :( But Twitter does it somehow.. – Ryan Bobrowski Jun 05 '15 at 14:55
  • that's definetely a non-trivial problem. but if I were you, I'd go for the in-memory solution for a first version of the app. this will save you days of development and bug fixing time!! and then, if you feel that the quality of the app can be drastically improved by adding a persistence layer, nothing shall stop you from implementing it later on :) – nburk Jun 05 '15 at 14:58
  • Last question xD For an initial/beta version of the app, it's generally acceptable not to have all of this stuff 100% optimized? I keep restarting the app because I want everything to be perfect: using the most efficient design patterns, checking for every possible problem, abstracting every piece of functionality...it's proving to be much more daunting than the PHP world that I'm used to. – Ryan Bobrowski Jun 05 '15 at 15:00
  • 1
    For simple persistance just use NSKeyedArchiver to serialize/deserialize data to/from file. I keep most of my data in dictionaries and it is very simple to sync to file with a few lines of code. – Rory McKinnel Jun 05 '15 at 15:00
  • 1
    haha... I mean also here it depends on the app and the context of your development. if it's a private project, don't spend too much time on polishing a 1.0. I know that getting the last 10% is usually the hardest part, but don't put too much pressure because "it has to be perfect"... it is perfectly legit to add fixes and improvements later on as the matures. of course, this will be different in a corporate context. – nburk Jun 05 '15 at 15:02
  • yeh...it's not a private app haha. – Ryan Bobrowski Jun 05 '15 at 15:03