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 !!

"Open In" for sharing in iOS Apps

Most of the iOS App developers may be wondering as why the most used and loved feature in Android "Open with" is not available in iOS. The reason for this being the way an application is structured in iOS. Files are encapsulated in an application environment called as Sand Box. So a file "F" downloaded using an app "A" belongs to "A" sandbox and hence is not visible to other apps on the iDevice. So lets consider a scenario where I have downloaded a RTF text file and I wanted to open it Evernote or any other app in my iDevice as developing my own file reader might not be an option always. How to accomplish this ? Have you noticed that DropBox does provide an option "Open In" to open any file in dropbox with other apps installed on the iDevice to do the same ?
UIDocumetInteractionController is the solution to this. I was developing an application where in I ran into similar situation and initially was deceived by UIActivityController to do the same. After pulling my hairs for couple of hours I did notice that  UIDocumetInteractionController is the solution to this and not UIActivityController. So below is the code as how to do this using a UIDocumetInteractionController.

// Declare a document interaction controller property

@property (retainUIDocumentInteractionController *docController;

NSURL *URL = [NSURL fileURLWithPath:filePath];
if (URL) {

        // Initialize Document Interaction Controller
        self.docController = [UIDocumentInteractionController interactionControllerWithURL:URL];

        // Configure Document Interaction Controller
        [self.docController setDelegate:self];

        // Present Open In Menu
inView:self.view animated:YES];

When this piece of code is executed the resulting output would be something as below based on the apps that are compatible to open the file installed on your iDevice.