SlideShare une entreprise Scribd logo
1  sur  129
Data Persistence in iOS
            Michał Tuszyński
       iOS/Android developer at appvetica




   @srgtuszy       srgtuszy.github.com
Roadmap
Roadmap
1. NSUserDefaults
Roadmap
1. NSUserDefaults
2. NSCache
Roadmap
1. NSUserDefaults
2. NSCache
3. NSCoding protocol
Roadmap
1. NSUserDefaults
2. NSCache
3. NSCoding protocol
4. CoreData
Roadmap
1. NSUserDefaults
2. NSCache
3. NSCoding protocol
4. CoreData
5. iCloud
What are
NSUserDefaults?
What are
           NSUserDefaults?

Key-value local storage, capable of storing both objects
                and primitive data types
What should not be stored in
     NSUserDefaults?
What should not be stored in
           NSUserDefaults?

1. Sensitive data
What should not be stored in
           NSUserDefaults?

1. Sensitive data
2. Heavy objects
What should not be stored in
           NSUserDefaults?

1. Sensitive data
2. Heavy objects
3. Big amounts of data
Demo
What if I need a mechanism to cache big
           amount of objects?
What if I need a mechanism to cache big
           amount of objects?


           ...use NSCache!
Why NSCache?
Why NSCache?
Similar api to NSMutableDictionary class
Why NSCache?
Similar api to NSMutableDictionary class


    - (void)setObject:(id)obj forKey:(id)key;
    - (void)removeObjectForKey:(id)key;
Why NSCache?

Why not just use NSMutableDictionary then?
Why NSCache?
Why not just use NSMutableDictionary then?
Why NSCache?
Why not just use NSMutableDictionary then?

   - NSCache takes care of memory management
Why NSCache?
Why not just use NSMutableDictionary then?

   - NSCache takes care of memory management
   - (void)setObject:(id)obj forKey:(id)key cost:(NSUInteger)num;
   - (void)setTotalCostLimit:(NSUInteger)lim;
Why NSCache?
Why not just use NSMutableDictionary then?

   - NSCache takes care of memory management
   - (void)setObject:(id)obj forKey:(id)key cost:(NSUInteger)num;
   - (void)setTotalCostLimit:(NSUInteger)lim;




   - Will let you know when it disposes data (NSCacheDelegate)
Why NSCache?
Why not just use NSMutableDictionary then?

   - NSCache takes care of memory management
   - (void)setObject:(id)obj forKey:(id)key cost:(NSUInteger)num;
   - (void)setTotalCostLimit:(NSUInteger)lim;




   - Will let you know when it disposes data (NSCacheDelegate)
   - (void)cache:(NSCache *)cache willEvictObject:(id)obj
Demo
How to store custom objects?
How to store custom objects?
         Using archives!
What are archives?
What are archives?
Archives provide a means to convert objects and values
    into architecture-independent stream of bytes
What are archives?
Archives provide a means to convert objects and values
    into architecture-independent stream of bytes
                      Apple documentation
Archives
Archives
Archives




Keyed archives
Archives




Keyed archives
Archives




Keyed archives              Sequential archives
What is the difference?
What is the difference?
What is the difference?

Sequential archives
What is the difference?

  Sequential archives
-Encoded from root object
through all interrelated objects
What is the difference?

  Sequential archives
-Encoded from root object
through all interrelated objects

-Must be decoded in the same
order it was encoded
What is the difference?

  Sequential archives              Keyed archives
-Encoded from root object
through all interrelated objects

-Must be decoded in the same
order it was encoded
What is the difference?

  Sequential archives                  Keyed archives
-Encoded from root object          -Each object is assigned a key
through all interrelated objects

-Must be decoded in the same
order it was encoded
What is the difference?

  Sequential archives                  Keyed archives
-Encoded from root object          -Each object is assigned a key
through all interrelated objects

-Must be decoded in the same
order it was encoded

      NSArchiver
What is the difference?

  Sequential archives                  Keyed archives
-Encoded from root object          -Each object is assigned a key
through all interrelated objects

-Must be decoded in the same
order it was encoded

      NSArchiver                        NSKeyedArchiver
How to create an archive out of a custom object?
How to create an archive out of a custom object?


 1.Implement NSCoding protocol inside the object
How to create an archive out of a custom object?


 1.Implement NSCoding protocol inside the object
            - (id)initWithCoder:(NSCoder *)decoder;

            - (void)encodeWithCoder:(NSCoder *)encoder;
How to create an archive out of a custom object?


 1.Implement NSCoding protocol inside the object
            - (id)initWithCoder:(NSCoder *)decoder;

            - (void)encodeWithCoder:(NSCoder *)encoder;




 2. Use one of the NSCoder subclasses to get your archive
Demo
Core Data
CoreData is an efficient framework to manage your object
                       persistence
CoreData is an efficient framework to manage your object
                          persistence

Main features:
CoreData is an efficient framework to manage your object
                           persistence

Main features:
-Change tracking and undo support
CoreData is an efficient framework to manage your object
                           persistence

Main features:
-Change tracking and undo support
-Relationship between entities maintenance
CoreData is an efficient framework to manage your object
                           persistence

Main features:
-Change tracking and undo support
-Relationship between entities maintenance
-Automatic validation of property values
CoreData is an efficient framework to manage your object
                           persistence

Main features:
-Change tracking and undo support
-Relationship between entities maintenance
-Automatic validation of property values
-Low memory overhead (Faulting)
What Core Data is not?
What Core Data is not?

-It is not a relational database
What Core Data is not?

-It is not a relational database
-It’s not a silver bullet for your code
How does it work?
How does it work?

      Storage
How does it work?

      Storage
How does it work?

          Storage




 NSPersistentStoreCoordinator
How does it work?

          Storage




                                The bridge between the storage
 NSPersistentStoreCoordinator         and the application
How does it work?

          Storage




                                The bridge between the storage
 NSPersistentStoreCoordinator         and the application
How does it work?

                                Storage




NSManagedObjectModel




                                                      The bridge between the storage
                       NSPersistentStoreCoordinator         and the application
How does it work?

                                              Storage




     NSManagedObjectModel



A collection of data models which
   are used in the application
                                                                    The bridge between the storage
                                     NSPersistentStoreCoordinator         and the application
How does it work?

                                              Storage




     NSManagedObjectModel



A collection of data models which
   are used in the application
                                                                    The bridge between the storage
                                     NSPersistentStoreCoordinator         and the application
How does it work?

                                              Storage




     NSManagedObjectModel



A collection of data models which
   are used in the application
                                                                    The bridge between the storage
                                     NSPersistentStoreCoordinator         and the application




                                      NSManagedObjectContext
How does it work?

                                              Storage




     NSManagedObjectModel



A collection of data models which
   are used in the application
                                                                    The bridge between the storage
                                     NSPersistentStoreCoordinator         and the application




                                                                     Scratchpad which communicates
                                                                                   with
                                      NSManagedObjectContext         NSPersistentStoreCoordinator to
                                                                               save changes
NSManagedObjectModel
NSManagedObjectModel
NSManagedObjectModel

  Entity
NSManagedObjectModel

  Entity




           Relationship
NSManagedObject
NSManagedObject
- Generic class that implements all basic behavior
required by CoreData model object
NSManagedObject
- Generic class that implements all basic behavior
required by CoreData model object
- It’s not possible to use regular NSObjects with
Core Data
NSManagedObject
- Generic class that implements all basic behavior
required by CoreData model object
- It’s not possible to use regular NSObjects with
Core Data
Can be created using following ways:
NSManagedObject
- Generic class that implements all basic behavior
required by CoreData model object
- It’s not possible to use regular NSObjects with
Core Data
Can be created using following ways:
1. Designated initializer
NSManagedObject
- Generic class that implements all basic behavior
required by CoreData model object
- It’s not possible to use regular NSObjects with
Core Data
Can be created using following ways:
1. Designated initializer
- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:
(NSManagedObjectContext *)context
NSManagedObject
- Generic class that implements all basic behavior
required by CoreData model object
- It’s not possible to use regular NSObjects with
Core Data
Can be created using following ways:
1. Designated initializer
- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:
(NSManagedObjectContext *)context


2. Static method of NSEntityDescription class
NSManagedObject
- Generic class that implements all basic behavior
required by CoreData model object
- It’s not possible to use regular NSObjects with
Core Data
Can be created using following ways:
1. Designated initializer
- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:
(NSManagedObjectContext *)context


2. Static method of NSEntityDescription class
+ (id)insertNewObjectForEntityForName:(NSString *)entityName inManagedObjectContext:
(NSManagedObjectContext *)context
Create NSManagedObjects to represent your data model


                   NSManagedObject
                    NSManagedObject
                     NSManagedObject
NSManagedObjects are held in
           NSManagedObjectContext’s cache

NSManagedObject
 NSManagedObject
  NSManagedObject
NSManagedObjects are held in
           NSManagedObjectContext’s cache

NSManagedObject
 NSManagedObject
  NSManagedObject
NSManagedObjects are held in
           NSManagedObjectContext’s cache

NSManagedObject
 NSManagedObject       NSManagedObjectContext
  NSManagedObject
NSManagedObjects are held in
           NSManagedObjectContext’s cache

NSManagedObject
 NSManagedObject       NSManagedObjectContext
  NSManagedObject
NSManagedObjects are held in
           NSManagedObjectContext’s cache

NSManagedObject
 NSManagedObject       NSManagedObjectContext
  NSManagedObject




                                                Save
Demo
iCloud
Why should you use iCloud
         APIs?
Why should you use iCloud
           APIs?
1. There’s no need to configure and maintain servers
Why should you use iCloud
            APIs?
1. There’s no need to configure and maintain servers
2. Syncs across all devices without your effort
Why should you use iCloud
            APIs?
1. There’s no need to configure and maintain servers
2. Syncs across all devices without your effort
3. Autosaving and conflict resolving
Why should you use iCloud
            APIs?
1. There’s no need to configure and maintain servers
2. Syncs across all devices without your effort
3. Autosaving and conflict resolving
4. Easily revert to a previous version
iCloud
iCloud
iCloud




Document storage
iCloud




Document storage
iCloud




Document storage            Key-value storage
Document storage

      Application
       sandbox
Document storage

Application
 sandbox
Document storage

Application   iCloud-monitored
 sandbox          directory
Document storage

Application                   iCloud-monitored
              NSFileManager
 sandbox                          directory
Document storage

Application                   iCloud-monitored
              NSFileManager
 sandbox                          directory
Document storage

              setUbiquitous:YES

Application                       iCloud-monitored
               NSFileManager
 sandbox                              directory
Document storage

              setUbiquitous:YES

Application                       iCloud-monitored
               NSFileManager
 sandbox                              directory
Document storage

              setUbiquitous:YES

Application                       iCloud-monitored
               NSFileManager
 sandbox                              directory

              setUbiquitous:NO
Limitations
Limitations
1. Does not work on simulator
Limitations
1. Does not work on simulator
2. A provisioning profile and entitlements are needed to test
Limitations
1. Does not work on simulator
2. A provisioning profile and entitlements are needed to test
3. All files stored in iCloud must be managed by an object that
implements NSFilePresenter protocol
Limitations
1. Does not work on simulator
2. A provisioning profile and entitlements are needed to test
3. All files stored in iCloud must be managed by an object that
implements NSFilePresenter protocol
UIKit provides UIDocument class which implements
NSFilePresenter protocol
Be responsible!
Be responsible!
1. Don’t store any auto generated files in iCloud.
Be responsible!
1. Don’t store any auto generated files in iCloud.
2. Don’t keep any copies of files tracked by iCloud.
Be responsible!
1. Don’t store any auto generated files in iCloud.
2. Don’t keep any copies of files tracked by iCloud.
3. Be careful with large files. Space is limited.
Be responsible!
1. Don’t store any auto generated files in iCloud.
2. Don’t keep any copies of files tracked by iCloud.
3. Be careful with large files. Space is limited.
4. Key-value storage has a limit of 64kb.
Demo
Where to go from here?
Where to go from here?
Where to go from here?




http://www.raywenderlich.com/934/core-data-tutorial-getting-started
Where to go from here?




http://www.raywenderlich.com/934/core-data-tutorial-getting-started
          https://developer.apple.com/videos/wwdc/2011/
Where to go from here?




http://www.raywenderlich.com/934/core-data-tutorial-getting-started
          https://developer.apple.com/videos/wwdc/2011/
  http://developer.apple.com/library/mac/#documentation/cocoa/
               Conceptual/Archiving/Archiving.html
Thank you!
https://github.com/Tuszy/UserDefaultsSample
https://github.com/Tuszy/NSCacheSample
https://github.com/Tuszy/ArchivesSample
https://github.com/Tuszy/CoreDataSample
https://github.com/Tuszy/iCloudSample

              @srgtuszy
         srgtuszy@gmail.com
          srgtuszy.github.com

Contenu connexe

Tendances

Hibernate jj
Hibernate jjHibernate jj
Hibernate jjJoe Jacob
 
Data Binding in Silverlight
Data Binding in SilverlightData Binding in Silverlight
Data Binding in SilverlightBoulos Dib
 
Hibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examplesHibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examplesEr. Gaurav Kumar
 
Spring (1)
Spring (1)Spring (1)
Spring (1)Aneega
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate TutorialRam132
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate FrameworkRaveendra R
 
Entity Framework Database and Code First
Entity Framework Database and Code FirstEntity Framework Database and Code First
Entity Framework Database and Code FirstJames Johnson
 
Introduction To Hibernate
Introduction To HibernateIntroduction To Hibernate
Introduction To Hibernateashishkulkarni
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepGuo Albert
 
Service Oriented Architecture -Unit II - Modeling databases in xml
Service Oriented Architecture -Unit II - Modeling databases in xml Service Oriented Architecture -Unit II - Modeling databases in xml
Service Oriented Architecture -Unit II - Modeling databases in xml Roselin Mary S
 
Entity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsEntity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsRichie Rump
 
Spring Data - Intro (Odessa Java TechTalks)
Spring Data - Intro (Odessa Java TechTalks)Spring Data - Intro (Odessa Java TechTalks)
Spring Data - Intro (Odessa Java TechTalks)Igor Anishchenko
 
For Beginners - Ado.net
For Beginners - Ado.netFor Beginners - Ado.net
For Beginners - Ado.netTarun Jain
 

Tendances (20)

Hibernate jj
Hibernate jjHibernate jj
Hibernate jj
 
Data Binding in Silverlight
Data Binding in SilverlightData Binding in Silverlight
Data Binding in Silverlight
 
Hibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examplesHibernate inheritance and relational mappings with examples
Hibernate inheritance and relational mappings with examples
 
Spring (1)
Spring (1)Spring (1)
Spring (1)
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate Tutorial
 
Jpa
JpaJpa
Jpa
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate Framework
 
Hibernate in Action
Hibernate in ActionHibernate in Action
Hibernate in Action
 
Entity Framework Database and Code First
Entity Framework Database and Code FirstEntity Framework Database and Code First
Entity Framework Database and Code First
 
Introduction To Hibernate
Introduction To HibernateIntroduction To Hibernate
Introduction To Hibernate
 
Hibernate 3
Hibernate 3Hibernate 3
Hibernate 3
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
 
Service Oriented Architecture -Unit II - Modeling databases in xml
Service Oriented Architecture -Unit II - Modeling databases in xml Service Oriented Architecture -Unit II - Modeling databases in xml
Service Oriented Architecture -Unit II - Modeling databases in xml
 
Introduction to Datastore
Introduction to DatastoreIntroduction to Datastore
Introduction to Datastore
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Ado.Net Tutorial
Ado.Net TutorialAdo.Net Tutorial
Ado.Net Tutorial
 
Entity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsEntity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic Unicorns
 
ADO.NET
ADO.NETADO.NET
ADO.NET
 
Spring Data - Intro (Odessa Java TechTalks)
Spring Data - Intro (Odessa Java TechTalks)Spring Data - Intro (Odessa Java TechTalks)
Spring Data - Intro (Odessa Java TechTalks)
 
For Beginners - Ado.net
For Beginners - Ado.netFor Beginners - Ado.net
For Beginners - Ado.net
 

Similaire à Data perisistance i_os

Connecting to a REST API in iOS
Connecting to a REST API in iOSConnecting to a REST API in iOS
Connecting to a REST API in iOSgillygize
 
Евгений Курпилянский "Индексирование поверх Cassandra". Выступление на Cassan...
Евгений Курпилянский "Индексирование поверх Cassandra". Выступление на Cassan...Евгений Курпилянский "Индексирование поверх Cassandra". Выступление на Cassan...
Евгений Курпилянский "Индексирование поверх Cassandra". Выступление на Cassan...it-people
 
Mashup iOS - Core data
Mashup iOS - Core dataMashup iOS - Core data
Mashup iOS - Core datassuser9ef26e
 
Core data in Swfit
Core data in SwfitCore data in Swfit
Core data in Swfitallanh0526
 
Intro to Core Data
Intro to Core DataIntro to Core Data
Intro to Core DataMake School
 
Core Data Migration
Core Data MigrationCore Data Migration
Core Data MigrationMonica Kurup
 
iOSDevCamp 2011 Core Data
iOSDevCamp 2011 Core DataiOSDevCamp 2011 Core Data
iOSDevCamp 2011 Core DataChris Mar
 
Mongo db transcript
Mongo db transcriptMongo db transcript
Mongo db transcriptfoliba
 
Spark 101 – First Steps To Distributed Computing - Demi Ben-Ari @ Ofek Alumni
Spark 101 – First Steps To Distributed Computing - Demi Ben-Ari @ Ofek AlumniSpark 101 – First Steps To Distributed Computing - Demi Ben-Ari @ Ofek Alumni
Spark 101 – First Steps To Distributed Computing - Demi Ben-Ari @ Ofek AlumniDemi Ben-Ari
 
CocoaHeads PDX 2014 01 23 : CoreData and iCloud Improvements iOS7 / OSX Maver...
CocoaHeads PDX 2014 01 23 : CoreData and iCloud Improvements iOS7 / OSX Maver...CocoaHeads PDX 2014 01 23 : CoreData and iCloud Improvements iOS7 / OSX Maver...
CocoaHeads PDX 2014 01 23 : CoreData and iCloud Improvements iOS7 / OSX Maver...smn-automate
 
Adventures in Multithreaded Core Data
Adventures in Multithreaded Core DataAdventures in Multithreaded Core Data
Adventures in Multithreaded Core DataInferis
 
Using MongoDB to Build a Fast and Scalable Content Repository
Using MongoDB to Build a Fast and Scalable Content RepositoryUsing MongoDB to Build a Fast and Scalable Content Repository
Using MongoDB to Build a Fast and Scalable Content RepositoryMongoDB
 
Spark Summit EU talk by Ross Lawley
Spark Summit EU talk by Ross LawleySpark Summit EU talk by Ross Lawley
Spark Summit EU talk by Ross LawleySpark Summit
 
How To Connect Spark To Your Own Datasource
How To Connect Spark To Your Own DatasourceHow To Connect Spark To Your Own Datasource
How To Connect Spark To Your Own DatasourceMongoDB
 

Similaire à Data perisistance i_os (20)

Connecting to a REST API in iOS
Connecting to a REST API in iOSConnecting to a REST API in iOS
Connecting to a REST API in iOS
 
Евгений Курпилянский "Индексирование поверх Cassandra". Выступление на Cassan...
Евгений Курпилянский "Индексирование поверх Cassandra". Выступление на Cassan...Евгений Курпилянский "Индексирование поверх Cassandra". Выступление на Cassan...
Евгений Курпилянский "Индексирование поверх Cassandra". Выступление на Cassan...
 
Mashup iOS - Core data
Mashup iOS - Core dataMashup iOS - Core data
Mashup iOS - Core data
 
Core data in Swfit
Core data in SwfitCore data in Swfit
Core data in Swfit
 
Intro to Core Data
Intro to Core DataIntro to Core Data
Intro to Core Data
 
Core Data Migration
Core Data MigrationCore Data Migration
Core Data Migration
 
iOSDevCamp 2011 Core Data
iOSDevCamp 2011 Core DataiOSDevCamp 2011 Core Data
iOSDevCamp 2011 Core Data
 
NoSql Databases
NoSql DatabasesNoSql Databases
NoSql Databases
 
Mongo db transcript
Mongo db transcriptMongo db transcript
Mongo db transcript
 
ORDBMS.pptx
ORDBMS.pptxORDBMS.pptx
ORDBMS.pptx
 
Spark 101 – First Steps To Distributed Computing - Demi Ben-Ari @ Ofek Alumni
Spark 101 – First Steps To Distributed Computing - Demi Ben-Ari @ Ofek AlumniSpark 101 – First Steps To Distributed Computing - Demi Ben-Ari @ Ofek Alumni
Spark 101 – First Steps To Distributed Computing - Demi Ben-Ari @ Ofek Alumni
 
CocoaHeads PDX 2014 01 23 : CoreData and iCloud Improvements iOS7 / OSX Maver...
CocoaHeads PDX 2014 01 23 : CoreData and iCloud Improvements iOS7 / OSX Maver...CocoaHeads PDX 2014 01 23 : CoreData and iCloud Improvements iOS7 / OSX Maver...
CocoaHeads PDX 2014 01 23 : CoreData and iCloud Improvements iOS7 / OSX Maver...
 
Adventures in Multithreaded Core Data
Adventures in Multithreaded Core DataAdventures in Multithreaded Core Data
Adventures in Multithreaded Core Data
 
Core data optimization
Core data optimizationCore data optimization
Core data optimization
 
Context at design
Context at designContext at design
Context at design
 
Rails meets no sql
Rails meets no sqlRails meets no sql
Rails meets no sql
 
Using MongoDB to Build a Fast and Scalable Content Repository
Using MongoDB to Build a Fast and Scalable Content RepositoryUsing MongoDB to Build a Fast and Scalable Content Repository
Using MongoDB to Build a Fast and Scalable Content Repository
 
Spark Summit EU talk by Ross Lawley
Spark Summit EU talk by Ross LawleySpark Summit EU talk by Ross Lawley
Spark Summit EU talk by Ross Lawley
 
How To Connect Spark To Your Own Datasource
How To Connect Spark To Your Own DatasourceHow To Connect Spark To Your Own Datasource
How To Connect Spark To Your Own Datasource
 
IDE.pptx
IDE.pptxIDE.pptx
IDE.pptx
 

Dernier

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 

Dernier (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

Data perisistance i_os

  • 1. Data Persistence in iOS Michał Tuszyński iOS/Android developer at appvetica @srgtuszy srgtuszy.github.com
  • 6. Roadmap 1. NSUserDefaults 2. NSCache 3. NSCoding protocol 4. CoreData
  • 7. Roadmap 1. NSUserDefaults 2. NSCache 3. NSCoding protocol 4. CoreData 5. iCloud
  • 9. What are NSUserDefaults? Key-value local storage, capable of storing both objects and primitive data types
  • 10. What should not be stored in NSUserDefaults?
  • 11. What should not be stored in NSUserDefaults? 1. Sensitive data
  • 12. What should not be stored in NSUserDefaults? 1. Sensitive data 2. Heavy objects
  • 13. What should not be stored in NSUserDefaults? 1. Sensitive data 2. Heavy objects 3. Big amounts of data
  • 14. Demo
  • 15. What if I need a mechanism to cache big amount of objects?
  • 16. What if I need a mechanism to cache big amount of objects? ...use NSCache!
  • 18. Why NSCache? Similar api to NSMutableDictionary class
  • 19. Why NSCache? Similar api to NSMutableDictionary class - (void)setObject:(id)obj forKey:(id)key; - (void)removeObjectForKey:(id)key;
  • 20. Why NSCache? Why not just use NSMutableDictionary then?
  • 21. Why NSCache? Why not just use NSMutableDictionary then?
  • 22. Why NSCache? Why not just use NSMutableDictionary then? - NSCache takes care of memory management
  • 23. Why NSCache? Why not just use NSMutableDictionary then? - NSCache takes care of memory management - (void)setObject:(id)obj forKey:(id)key cost:(NSUInteger)num; - (void)setTotalCostLimit:(NSUInteger)lim;
  • 24. Why NSCache? Why not just use NSMutableDictionary then? - NSCache takes care of memory management - (void)setObject:(id)obj forKey:(id)key cost:(NSUInteger)num; - (void)setTotalCostLimit:(NSUInteger)lim; - Will let you know when it disposes data (NSCacheDelegate)
  • 25. Why NSCache? Why not just use NSMutableDictionary then? - NSCache takes care of memory management - (void)setObject:(id)obj forKey:(id)key cost:(NSUInteger)num; - (void)setTotalCostLimit:(NSUInteger)lim; - Will let you know when it disposes data (NSCacheDelegate) - (void)cache:(NSCache *)cache willEvictObject:(id)obj
  • 26. Demo
  • 27. How to store custom objects?
  • 28. How to store custom objects? Using archives!
  • 30. What are archives? Archives provide a means to convert objects and values into architecture-independent stream of bytes
  • 31. What are archives? Archives provide a means to convert objects and values into architecture-independent stream of bytes Apple documentation
  • 32.
  • 37. Archives Keyed archives Sequential archives
  • 38. What is the difference?
  • 39. What is the difference?
  • 40. What is the difference? Sequential archives
  • 41. What is the difference? Sequential archives -Encoded from root object through all interrelated objects
  • 42. What is the difference? Sequential archives -Encoded from root object through all interrelated objects -Must be decoded in the same order it was encoded
  • 43. What is the difference? Sequential archives Keyed archives -Encoded from root object through all interrelated objects -Must be decoded in the same order it was encoded
  • 44. What is the difference? Sequential archives Keyed archives -Encoded from root object -Each object is assigned a key through all interrelated objects -Must be decoded in the same order it was encoded
  • 45. What is the difference? Sequential archives Keyed archives -Encoded from root object -Each object is assigned a key through all interrelated objects -Must be decoded in the same order it was encoded NSArchiver
  • 46. What is the difference? Sequential archives Keyed archives -Encoded from root object -Each object is assigned a key through all interrelated objects -Must be decoded in the same order it was encoded NSArchiver NSKeyedArchiver
  • 47. How to create an archive out of a custom object?
  • 48. How to create an archive out of a custom object? 1.Implement NSCoding protocol inside the object
  • 49. How to create an archive out of a custom object? 1.Implement NSCoding protocol inside the object - (id)initWithCoder:(NSCoder *)decoder; - (void)encodeWithCoder:(NSCoder *)encoder;
  • 50. How to create an archive out of a custom object? 1.Implement NSCoding protocol inside the object - (id)initWithCoder:(NSCoder *)decoder; - (void)encodeWithCoder:(NSCoder *)encoder; 2. Use one of the NSCoder subclasses to get your archive
  • 51. Demo
  • 52.
  • 54. CoreData is an efficient framework to manage your object persistence
  • 55. CoreData is an efficient framework to manage your object persistence Main features:
  • 56. CoreData is an efficient framework to manage your object persistence Main features: -Change tracking and undo support
  • 57. CoreData is an efficient framework to manage your object persistence Main features: -Change tracking and undo support -Relationship between entities maintenance
  • 58. CoreData is an efficient framework to manage your object persistence Main features: -Change tracking and undo support -Relationship between entities maintenance -Automatic validation of property values
  • 59. CoreData is an efficient framework to manage your object persistence Main features: -Change tracking and undo support -Relationship between entities maintenance -Automatic validation of property values -Low memory overhead (Faulting)
  • 60. What Core Data is not?
  • 61. What Core Data is not? -It is not a relational database
  • 62. What Core Data is not? -It is not a relational database -It’s not a silver bullet for your code
  • 63. How does it work?
  • 64. How does it work? Storage
  • 65. How does it work? Storage
  • 66. How does it work? Storage NSPersistentStoreCoordinator
  • 67. How does it work? Storage The bridge between the storage NSPersistentStoreCoordinator and the application
  • 68. How does it work? Storage The bridge between the storage NSPersistentStoreCoordinator and the application
  • 69. How does it work? Storage NSManagedObjectModel The bridge between the storage NSPersistentStoreCoordinator and the application
  • 70. How does it work? Storage NSManagedObjectModel A collection of data models which are used in the application The bridge between the storage NSPersistentStoreCoordinator and the application
  • 71. How does it work? Storage NSManagedObjectModel A collection of data models which are used in the application The bridge between the storage NSPersistentStoreCoordinator and the application
  • 72. How does it work? Storage NSManagedObjectModel A collection of data models which are used in the application The bridge between the storage NSPersistentStoreCoordinator and the application NSManagedObjectContext
  • 73. How does it work? Storage NSManagedObjectModel A collection of data models which are used in the application The bridge between the storage NSPersistentStoreCoordinator and the application Scratchpad which communicates with NSManagedObjectContext NSPersistentStoreCoordinator to save changes
  • 79. NSManagedObject - Generic class that implements all basic behavior required by CoreData model object
  • 80. NSManagedObject - Generic class that implements all basic behavior required by CoreData model object - It’s not possible to use regular NSObjects with Core Data
  • 81. NSManagedObject - Generic class that implements all basic behavior required by CoreData model object - It’s not possible to use regular NSObjects with Core Data Can be created using following ways:
  • 82. NSManagedObject - Generic class that implements all basic behavior required by CoreData model object - It’s not possible to use regular NSObjects with Core Data Can be created using following ways: 1. Designated initializer
  • 83. NSManagedObject - Generic class that implements all basic behavior required by CoreData model object - It’s not possible to use regular NSObjects with Core Data Can be created using following ways: 1. Designated initializer - (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext: (NSManagedObjectContext *)context
  • 84. NSManagedObject - Generic class that implements all basic behavior required by CoreData model object - It’s not possible to use regular NSObjects with Core Data Can be created using following ways: 1. Designated initializer - (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext: (NSManagedObjectContext *)context 2. Static method of NSEntityDescription class
  • 85. NSManagedObject - Generic class that implements all basic behavior required by CoreData model object - It’s not possible to use regular NSObjects with Core Data Can be created using following ways: 1. Designated initializer - (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext: (NSManagedObjectContext *)context 2. Static method of NSEntityDescription class + (id)insertNewObjectForEntityForName:(NSString *)entityName inManagedObjectContext: (NSManagedObjectContext *)context
  • 86. Create NSManagedObjects to represent your data model NSManagedObject NSManagedObject NSManagedObject
  • 87. NSManagedObjects are held in NSManagedObjectContext’s cache NSManagedObject NSManagedObject NSManagedObject
  • 88. NSManagedObjects are held in NSManagedObjectContext’s cache NSManagedObject NSManagedObject NSManagedObject
  • 89. NSManagedObjects are held in NSManagedObjectContext’s cache NSManagedObject NSManagedObject NSManagedObjectContext NSManagedObject
  • 90. NSManagedObjects are held in NSManagedObjectContext’s cache NSManagedObject NSManagedObject NSManagedObjectContext NSManagedObject
  • 91. NSManagedObjects are held in NSManagedObjectContext’s cache NSManagedObject NSManagedObject NSManagedObjectContext NSManagedObject Save
  • 92. Demo
  • 93.
  • 95. Why should you use iCloud APIs?
  • 96. Why should you use iCloud APIs? 1. There’s no need to configure and maintain servers
  • 97. Why should you use iCloud APIs? 1. There’s no need to configure and maintain servers 2. Syncs across all devices without your effort
  • 98. Why should you use iCloud APIs? 1. There’s no need to configure and maintain servers 2. Syncs across all devices without your effort 3. Autosaving and conflict resolving
  • 99. Why should you use iCloud APIs? 1. There’s no need to configure and maintain servers 2. Syncs across all devices without your effort 3. Autosaving and conflict resolving 4. Easily revert to a previous version
  • 100. iCloud
  • 101. iCloud
  • 104. iCloud Document storage Key-value storage
  • 105. Document storage Application sandbox
  • 107. Document storage Application iCloud-monitored sandbox directory
  • 108. Document storage Application iCloud-monitored NSFileManager sandbox directory
  • 109. Document storage Application iCloud-monitored NSFileManager sandbox directory
  • 110. Document storage setUbiquitous:YES Application iCloud-monitored NSFileManager sandbox directory
  • 111. Document storage setUbiquitous:YES Application iCloud-monitored NSFileManager sandbox directory
  • 112. Document storage setUbiquitous:YES Application iCloud-monitored NSFileManager sandbox directory setUbiquitous:NO
  • 114. Limitations 1. Does not work on simulator
  • 115. Limitations 1. Does not work on simulator 2. A provisioning profile and entitlements are needed to test
  • 116. Limitations 1. Does not work on simulator 2. A provisioning profile and entitlements are needed to test 3. All files stored in iCloud must be managed by an object that implements NSFilePresenter protocol
  • 117. Limitations 1. Does not work on simulator 2. A provisioning profile and entitlements are needed to test 3. All files stored in iCloud must be managed by an object that implements NSFilePresenter protocol UIKit provides UIDocument class which implements NSFilePresenter protocol
  • 119. Be responsible! 1. Don’t store any auto generated files in iCloud.
  • 120. Be responsible! 1. Don’t store any auto generated files in iCloud. 2. Don’t keep any copies of files tracked by iCloud.
  • 121. Be responsible! 1. Don’t store any auto generated files in iCloud. 2. Don’t keep any copies of files tracked by iCloud. 3. Be careful with large files. Space is limited.
  • 122. Be responsible! 1. Don’t store any auto generated files in iCloud. 2. Don’t keep any copies of files tracked by iCloud. 3. Be careful with large files. Space is limited. 4. Key-value storage has a limit of 64kb.
  • 123. Demo
  • 124. Where to go from here?
  • 125. Where to go from here?
  • 126. Where to go from here? http://www.raywenderlich.com/934/core-data-tutorial-getting-started
  • 127. Where to go from here? http://www.raywenderlich.com/934/core-data-tutorial-getting-started https://developer.apple.com/videos/wwdc/2011/
  • 128. Where to go from here? http://www.raywenderlich.com/934/core-data-tutorial-getting-started https://developer.apple.com/videos/wwdc/2011/ http://developer.apple.com/library/mac/#documentation/cocoa/ Conceptual/Archiving/Archiving.html

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. \n
  76. \n
  77. \n
  78. \n
  79. \n
  80. \n
  81. \n
  82. \n
  83. \n
  84. \n
  85. \n
  86. \n
  87. \n
  88. \n
  89. \n
  90. \n
  91. \n
  92. \n
  93. \n
  94. \n
  95. \n
  96. \n
  97. \n
  98. \n
  99. \n
  100. \n
  101. \n
  102. \n
  103. \n
  104. \n
  105. \n