Sunday, 27 April 2014

Post WDI - iPhone App Sandbox, Data Management Persistence Mechanisms (Object Archiving, Core Data to SQLite)

iPhone Development (Xcode 5) (Cont'd)

iPhone App Sandbox and Data Management  DONE
  • Application Sandbox { restrictions (policy and technical) introduced by Apple to protect users from malicious apps harming their devices, and where all apps exist in a sandbox. violation cause for app rejection from App Store }
  • Object Archiving 
    • Object Graph { all references between interlinked web of objects in memory. used for Cocoa's Object Archiving (serialisation mechanism) where the object graph is stored to the file system and unarchived and read it back into memory to restore a session at the same point. Object Archiving Components include NSCoder object and NSCoding Protocol }
      • NSCoder { encode and decode archiving any object to NSCoding Protocol using "keyed encoding" where a key is provided for each instance var }
      • NSCoding Protocol 
      • Methods: 
        • initWithCoder { Decoding where key is provided and decoded instance var returned } 
        • encodeWithCoder { Encode all instance vars (using encodeObject:forKey, encodeInt:forKey, encodeBool:forKey, encodeDouble:forKey, encodeBytes:forKey) of object to store during archival. implemented with decision on instance vars of scalar type to encode or not encode (transient) } 
    • Archiving to File
      • archiveRootObject:toFile of NSKeyedArchiver (on applicationWillTerminate event)
    • Unarchiving from File
      • unarchiveObjectWithFile method from NSKeyedUnarchiver(on viewDidLoad event)
    • Data Storage Options
      • Library/Preferences Directory { access indirectly with NSUserDefaults API }
      • Library/Caches Directory { direct file manipulation, cache retrieved data, persists between launches, improves performance }
      • Documents Directory { direct file manipulation, main location to store, backed up when synced }
      • tmp Directory { direct file manipulation outside devices limited volatile memory without persisting between launches of app }
    • File Paths 
      • NSSearchPathForDirectoriesInDomains { Core Foundation function returns multiple directories in NSArray with NSStrings extracted with objectAtIndex }
      • stringByAppendingPathComponent { joins path fragments }
    • Interface Elements
      • Flexible Space Bar Button
  • Core Data 
    • Dfn: Object-Relational Mapping (ORM) framework (handles conversion of data to and from objects in the app and relational data in a database) providing management and persistence of in-memory object graphs comprising multi-level undo management, data validation, data consistency across independent data accessors, efficient filtering with indexing, sorting, and searching object graphs, and persistence between invocations to various data repositories 
    • Uses: wraps the SQLite database and relational database (alternative to using SQL and SQLite API directly) to perform querying and data management functions of CRUD (create, read, update, delete)
    • Alternatives to SQLite Repository 
      • Uses: Core Data features (undo support, object schema versioning) without need for relational databases
      • Binary File Format { same limits on large data sets as Object Archiving }
      • In-Memory Implementation { no persistence between app invocations }
    • Concepts of Core Data
      • Managed Object Context { subset of persistent objects used by app at a point in time and containing the object graph. Core Data notices changes to the objects and persists changes to the database when save is called }
      • Managed Object { object managed by Core Data that can be added or removed from object graph resulting in Core Data performing inserts and deletes w database. changes to attributes and relationships of managed objects results in Core Data performing update operations to database }
      • Managed Object Model { object-relational schema, description of Entities persisted via Core Data. it's a mapping between an object in Objective-C and an Entity as it will be stored as rows in a database table in SQLite }
        • Xcode Data Modelling Tool { for .xcdatamodel files }
      • Entity 
        • Dfn: model of managed object containing only parts of object that Core Data will be persisting
        • Model Types:
          • Object Attributes { for persisting }
          • Relationships and Other Entities { stored to form a persistent object graph }
      • Persistent Stack { parts of Core Data that interact with a data repository like SQLite }
    • Core Data Application Template 
      • Dfn: boilerplate code that contains additional properties and actions to allow the Core Data application to interact with the managed object context through its application delegate 
      • Features:
        • @dynamic construct { tells compiler missing property getters and setters in implementation file will be provided by superclass NSManagedObject at runtime } 
        • managedObjectContext property { Core Data managed object context for passing to controllers subview in the app window upon applicationDidFinishLaunching }
      • CRUD (Create, Read, Update, Delete)
        • insertNewObjectForEntityForName:inManagedObjectContext method of NSEntityDescription { Create. arguments include name of Entity NSString from managed object model, and reference to managed object context }
        • NSFetchRequest instance in viewDidLoad method { Fetch. with NSEntityDescription to specify type of Entity to fetch, returns NSMutableArray of managed objects
        • hasChanges used by managed object context to report any changes to managed objects that were tracked and save in the applicationWillTerminate method 
        • deleteObject method of NSManagedObjectContext is passed managed object to delete
        • Note: NSUInteger vs NSNumber (scalar integer vs actual object)


No comments:

Post a Comment