Wednesday, June 11, 2014

CoreData - Do you see a snag ?

I some how have got to hear this in multiple forums at multiple stages. Especially in interviews where it generally starts with "How comfortable are you with Core Data ?" and ends with "Aware of any draw backs with Core Data ?".

So, here I write up a small article to build up the repertoire of those who are gearing up for a switch and are apprehensive of the interviews and the other set of people who had to sit blank when this was hurled at them in their interviews and could not help other than ducking this question and googling the same.

   The one word answer to this, the word that you can emphasize and stress on to save you is "In - Memory". You can get away easily with a single statement "Core Data is In - Memory". Now if you want to proceed with this to further strengthen your explanation and convince the interviewer, below is the explanation for you with a case considered.

Use - Case

Lets consider the case of a collage making app. The maximum images that can be allowed per collage be 8-10. Since the maximum number of images being used is already a limit imposed lets not limit the size of image.

The app is expected to support both online and offline mode. Offline mode should let the user see the collages he has created already though creating new collage may be restricted to online mode.

If the user has created a collage of 8 images each of which is around 3-4 MB lets consider. To support offline viewing of the collage thereby created we need to store these images in our app somewhere. Now the question arises as where do I store it and how ? As Core Data is the topic in discussion, lets consider we use Core - Data.

How heavy the operation would be for your persistent coordinator to fetch these images from Core Data and render them in you image views of the collage ?

Would not the App crash in a compact environment giving a memory exception ?
This is where you have to start counting on other ways of storing data for offline rendering.

If not Core - Dat what would be the other option to this case ?

Yes. NSDocumentsDirectory would fit in the need. Having your images stored in NSDocuments Directory would provide you the way to accomplish the offline functioning.

        As expected the other best answer which every iOS developer comes up with would be Thread Safety. Core Data managed object context not being Thread safe is one highlighting point for every developer scratching their heads and cursing Core - Data at times when complex threaded DB operations are to be performed. But before pointing at this, is any other framework in Cocoa Thread safe ? So is it justified to highlight managed object context not being thread safe as a snag of Core -Data just because resolving conflicts with Core-Data is a bit cumbersome when compared to other frameworks. I will some day post an article on Core Data - Thread Safety too.

 Cheers !!

No comments:

Post a Comment