Monday, September 6, 2010

Core Data -1

When it comes to data management,we would generally use a file or a Sqlite Db to maintain persistent data.We would manually write the algorithm to save data/read to/from the persistent store into the memory and back.

Core data provides us a pre-packaged framework to create Models/Entities and API to persist
them on our devices.

When we create an Iphone App and check the coredata option the following 3 references are created in
the Application Delegate :

-NSManagedObjectModel *managedObjectModel;
- NSManagedObjectContext *managedObjectContext;
- NSPersistentStoreCoordinator *persistentStoreCoordinator;


There are 3 Layers in CoreDate architecture :
1. NSPeristantStoreCoordinator
    a.Responsibile for the direct link with the underlying physical store using NSURL
                    NSURL *storeUrl = [NSURL     fileURLWithPath:[[self applicationDocumentsDirectory]
                     stringByAppendingPathComponent: @"db.sqlite" ]];
                    
2.NSManagedObjectModel
        It contains the metadata for the model which in turn will contain the entities.
        The entity can be visualized as a class file which will replicate the database
        object in memory
   
3.NSManagedObjectContext
        The Managed Object Context(MOC) is used as a scratch pad.
        Objects are pulled through the stack into the
        MOC and then kept there while we change them. All inserts, deletes and
        updates to the set of objects in the MOC are held until we tell the MOC
        to save. At that point the MOC’s list of changes is pushed down through
        the stack, at each step translated closer to the eventual language of the
        POS(Persistant Object Store eg.File/SqlLite where it eventually becomes native
        (i.e. SQL statements for the SQLite POS) and sent to the persistent storage.
       
       
A.    Steps involved in creating a PersistentStoreCoordinator

    -(NSPersistentStoreCorordinator *) persistentStoreCoordinator
    {
        if(!persistentStoreCoordinator) return persistentStoreCoordinator;

        NSURL *storeUrl =[NSURL fileURLWithPath:[[self applicationDocumentDirectory] 
                stringByAppendingPathComponent:@"db.sqlite"]];

        NSError *error=nil;
        persistentStoreCoordinator =[[NSPersistentStoreCorordinator alloc]
                        initWithObjectModel:[self managedObjectModel]];

        if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                    configuration:nil
                    URL:storeUrl
                    options:nil
                    error:&error])
        {
            //handle errors
        }

        return persistentStoreCoordinator;
    }

    (NSString *)applicationDocumentsDirectory {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSString *basePath = nil;
        if([paths count] > 0)
        {
            basePath = [paths objectAtIndex:0];
        }
        return basePath;
    }

B.     Steps involved in creating the ManagedObjectModel which will store all our entities
    -(NSManagedObjectModel *) managedObjectModel
    {
        if(managedObjectModel!=nil)
            return managedObjectModel;
       
        //This will create one MOM for all model files in our application
        managedObjectModel=[[NSManagedObjectModel mergedModelFromBundles:nil] retain];
       
        return managedObjectModel;
    }
   




C.    Steps involved in creating the ManagedObjectContext
    i.  Get the persistent store coordinator
    ii. Configure the ManagedObjectContext
   
    - (NSManagedObjectContext *) managedObjectContext
    {
        if (managedObjectContext != nil)    
            return managedObjectContext;
       
        NSPersistentStoreCoordinator *coordinator=[self persistentStoreCoordinator];
        if(coordinator != nil)
        {   
            managedObjectContext=[[NSManagedObjectContext alloc] init];
            [managedObjectContext setPersistentCoordinator:coordinator];
        }
        return     managedObjectContext;
    }
  

No comments:

Post a Comment