0
Data Persistence in iOS            Michał Tuszyński       iOS/Android developer at appvetica   @srgtuszy       srgtuszy.gi...
Roadmap
Roadmap1. NSUserDefaults
Roadmap1. NSUserDefaults2. NSCache
Roadmap1. NSUserDefaults2. NSCache3. NSCoding protocol
Roadmap1. NSUserDefaults2. NSCache3. NSCoding protocol4. CoreData
Roadmap1. NSUserDefaults2. NSCache3. NSCoding protocol4. CoreData5. iCloud
What areNSUserDefaults?
What are           NSUserDefaults?Key-value local storage, capable of storing both objects                and primitive da...
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 data2. Heavy objects
What should not be stored in           NSUserDefaults?1. Sensitive data2. Heavy objects3. 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)removeObjectF...
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...
Why NSCache?Why not just use NSMutableDictionary then?   - NSCache takes care of memory management   - (void)setObject:(id...
Why NSCache?Why not just use NSMutableDictionary then?   - NSCache takes care of memory management   - (void)setObject:(id...
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 ...
Archives
Archives
ArchivesKeyed archives
ArchivesKeyed archives
ArchivesKeyed 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 objectthrough all interrelated objects
What is the difference?  Sequential archives-Encoded from root objectthrough all interrelated objects-Must be decoded in t...
What is the difference?  Sequential archives              Keyed archives-Encoded from root objectthrough all interrelated ...
What is the difference?  Sequential archives                  Keyed archives-Encoded from root object          -Each objec...
What is the difference?  Sequential archives                  Keyed archives-Encoded from root object          -Each objec...
What is the difference?  Sequential archives                  Keyed archives-Encoded from root object          -Each objec...
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)initWith...
How to create an archive out of a custom object? 1.Implement NSCoding protocol inside the object            - (id)initWith...
Demo
Core Data
CoreData is an efficient framework to manage your object                       persistence
CoreData is an efficient framework to manage your object                          persistenceMain features:
CoreData is an efficient framework to manage your object                           persistenceMain features:-Change trackin...
CoreData is an efficient framework to manage your object                           persistenceMain features:-Change trackin...
CoreData is an efficient framework to manage your object                           persistenceMain features:-Change trackin...
CoreData is an efficient framework to manage your object                           persistenceMain features:-Change trackin...
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 NSPersistentStoreCoordina...
How does it work?          Storage                                The bridge between the storage NSPersistentStoreCoordina...
How does it work?                                StorageNSManagedObjectModel                                              ...
How does it work?                                              Storage     NSManagedObjectModelA collection of data models...
How does it work?                                              Storage     NSManagedObjectModelA collection of data models...
How does it work?                                              Storage     NSManagedObjectModelA collection of data models...
How does it work?                                              Storage     NSManagedObjectModelA collection of data models...
NSManagedObjectModel
NSManagedObjectModel
NSManagedObjectModel  Entity
NSManagedObjectModel  Entity           Relationship
NSManagedObject
NSManagedObject- Generic class that implements all basic behaviorrequired by CoreData model object
NSManagedObject- Generic class that implements all basic behaviorrequired by CoreData model object- It’s not possible to u...
NSManagedObject- Generic class that implements all basic behaviorrequired by CoreData model object- It’s not possible to u...
NSManagedObject- Generic class that implements all basic behaviorrequired by CoreData model object- It’s not possible to u...
NSManagedObject- Generic class that implements all basic behaviorrequired by CoreData model object- It’s not possible to u...
NSManagedObject- Generic class that implements all basic behaviorrequired by CoreData model object- It’s not possible to u...
NSManagedObject- Generic class that implements all basic behaviorrequired by CoreData model object- It’s not possible to u...
Create NSManagedObjects to represent your data model                   NSManagedObject                    NSManagedObject ...
NSManagedObjects are held in           NSManagedObjectContext’s cacheNSManagedObject NSManagedObject  NSManagedObject
NSManagedObjects are held in           NSManagedObjectContext’s cacheNSManagedObject NSManagedObject  NSManagedObject
NSManagedObjects are held in           NSManagedObjectContext’s cacheNSManagedObject NSManagedObject       NSManagedObject...
NSManagedObjects are held in           NSManagedObjectContext’s cacheNSManagedObject NSManagedObject       NSManagedObject...
NSManagedObjects are held in           NSManagedObjectContext’s cacheNSManagedObject NSManagedObject       NSManagedObject...
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 servers2. Syncs across all devices w...
Why should you use iCloud            APIs?1. There’s no need to configure and maintain servers2. Syncs across all devices w...
Why should you use iCloud            APIs?1. There’s no need to configure and maintain servers2. Syncs across all devices w...
iCloud
iCloud
iCloudDocument storage
iCloudDocument storage
iCloudDocument storage            Key-value storage
Document storage      Application       sandbox
Document storageApplication sandbox
Document storageApplication   iCloud-monitored sandbox          directory
Document storageApplication                   iCloud-monitored              NSFileManager sandbox                         ...
Document storageApplication                   iCloud-monitored              NSFileManager sandbox                         ...
Document storage              setUbiquitous:YESApplication                       iCloud-monitored               NSFileMana...
Document storage              setUbiquitous:YESApplication                       iCloud-monitored               NSFileMana...
Document storage              setUbiquitous:YESApplication                       iCloud-monitored               NSFileMana...
Limitations
Limitations1. Does not work on simulator
Limitations1. Does not work on simulator2. A provisioning profile and entitlements are needed to test
Limitations1. Does not work on simulator2. A provisioning profile and entitlements are needed to test3. All files stored in ...
Limitations1. Does not work on simulator2. A provisioning profile and entitlements are needed to test3. All files stored in ...
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 c...
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 c...
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...
Where to go from here?http://www.raywenderlich.com/934/core-data-tutorial-getting-started          https://developer.apple...
Thank you!https://github.com/Tuszy/UserDefaultsSamplehttps://github.com/Tuszy/NSCacheSamplehttps://github.com/Tuszy/Archiv...
Data perisistance i_os
Data perisistance i_os
Data perisistance i_os
Upcoming SlideShare
Loading in...5
×

Data perisistance i_os

805

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
805
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
35
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript of "Data perisistance i_os"

    1. 1. Data Persistence in iOS Michał Tuszyński iOS/Android developer at appvetica @srgtuszy srgtuszy.github.com
    2. 2. Roadmap
    3. 3. Roadmap1. NSUserDefaults
    4. 4. Roadmap1. NSUserDefaults2. NSCache
    5. 5. Roadmap1. NSUserDefaults2. NSCache3. NSCoding protocol
    6. 6. Roadmap1. NSUserDefaults2. NSCache3. NSCoding protocol4. CoreData
    7. 7. Roadmap1. NSUserDefaults2. NSCache3. NSCoding protocol4. CoreData5. iCloud
    8. 8. What areNSUserDefaults?
    9. 9. What are NSUserDefaults?Key-value local storage, capable of storing both objects and primitive data types
    10. 10. What should not be stored in NSUserDefaults?
    11. 11. What should not be stored in NSUserDefaults?1. Sensitive data
    12. 12. What should not be stored in NSUserDefaults?1. Sensitive data2. Heavy objects
    13. 13. What should not be stored in NSUserDefaults?1. Sensitive data2. Heavy objects3. Big amounts of data
    14. 14. Demo
    15. 15. What if I need a mechanism to cache big amount of objects?
    16. 16. What if I need a mechanism to cache big amount of objects? ...use NSCache!
    17. 17. Why NSCache?
    18. 18. Why NSCache?Similar api to NSMutableDictionary class
    19. 19. Why NSCache?Similar api to NSMutableDictionary class - (void)setObject:(id)obj forKey:(id)key; - (void)removeObjectForKey:(id)key;
    20. 20. Why NSCache?Why not just use NSMutableDictionary then?
    21. 21. Why NSCache?Why not just use NSMutableDictionary then?
    22. 22. Why NSCache?Why not just use NSMutableDictionary then? - NSCache takes care of memory management
    23. 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. 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. 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. 26. Demo
    27. 27. How to store custom objects?
    28. 28. How to store custom objects? Using archives!
    29. 29. What are archives?
    30. 30. What are archives?Archives provide a means to convert objects and values into architecture-independent stream of bytes
    31. 31. What are archives?Archives provide a means to convert objects and values into architecture-independent stream of bytes Apple documentation
    32. 32. Archives
    33. 33. Archives
    34. 34. ArchivesKeyed archives
    35. 35. ArchivesKeyed archives
    36. 36. ArchivesKeyed archives Sequential archives
    37. 37. What is the difference?
    38. 38. What is the difference?
    39. 39. What is the difference?Sequential archives
    40. 40. What is the difference? Sequential archives-Encoded from root objectthrough all interrelated objects
    41. 41. What is the difference? Sequential archives-Encoded from root objectthrough all interrelated objects-Must be decoded in the sameorder it was encoded
    42. 42. What is the difference? Sequential archives Keyed archives-Encoded from root objectthrough all interrelated objects-Must be decoded in the sameorder it was encoded
    43. 43. What is the difference? Sequential archives Keyed archives-Encoded from root object -Each object is assigned a keythrough all interrelated objects-Must be decoded in the sameorder it was encoded
    44. 44. What is the difference? Sequential archives Keyed archives-Encoded from root object -Each object is assigned a keythrough all interrelated objects-Must be decoded in the sameorder it was encoded NSArchiver
    45. 45. What is the difference? Sequential archives Keyed archives-Encoded from root object -Each object is assigned a keythrough all interrelated objects-Must be decoded in the sameorder it was encoded NSArchiver NSKeyedArchiver
    46. 46. How to create an archive out of a custom object?
    47. 47. How to create an archive out of a custom object? 1.Implement NSCoding protocol inside the object
    48. 48. 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;
    49. 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; 2. Use one of the NSCoder subclasses to get your archive
    50. 50. Demo
    51. 51. Core Data
    52. 52. CoreData is an efficient framework to manage your object persistence
    53. 53. CoreData is an efficient framework to manage your object persistenceMain features:
    54. 54. CoreData is an efficient framework to manage your object persistenceMain features:-Change tracking and undo support
    55. 55. CoreData is an efficient framework to manage your object persistenceMain features:-Change tracking and undo support-Relationship between entities maintenance
    56. 56. CoreData is an efficient framework to manage your object persistenceMain features:-Change tracking and undo support-Relationship between entities maintenance-Automatic validation of property values
    57. 57. CoreData is an efficient framework to manage your object persistenceMain features:-Change tracking and undo support-Relationship between entities maintenance-Automatic validation of property values-Low memory overhead (Faulting)
    58. 58. What Core Data is not?
    59. 59. What Core Data is not?-It is not a relational database
    60. 60. What Core Data is not?-It is not a relational database-It’s not a silver bullet for your code
    61. 61. How does it work?
    62. 62. How does it work? Storage
    63. 63. How does it work? Storage
    64. 64. How does it work? Storage NSPersistentStoreCoordinator
    65. 65. How does it work? Storage The bridge between the storage NSPersistentStoreCoordinator and the application
    66. 66. How does it work? Storage The bridge between the storage NSPersistentStoreCoordinator and the application
    67. 67. How does it work? StorageNSManagedObjectModel The bridge between the storage NSPersistentStoreCoordinator and the application
    68. 68. How does it work? Storage NSManagedObjectModelA collection of data models which are used in the application The bridge between the storage NSPersistentStoreCoordinator and the application
    69. 69. How does it work? Storage NSManagedObjectModelA collection of data models which are used in the application The bridge between the storage NSPersistentStoreCoordinator and the application
    70. 70. How does it work? Storage NSManagedObjectModelA collection of data models which are used in the application The bridge between the storage NSPersistentStoreCoordinator and the application NSManagedObjectContext
    71. 71. How does it work? Storage NSManagedObjectModelA 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
    72. 72. NSManagedObjectModel
    73. 73. NSManagedObjectModel
    74. 74. NSManagedObjectModel Entity
    75. 75. NSManagedObjectModel Entity Relationship
    76. 76. NSManagedObject
    77. 77. NSManagedObject- Generic class that implements all basic behaviorrequired by CoreData model object
    78. 78. NSManagedObject- Generic class that implements all basic behaviorrequired by CoreData model object- It’s not possible to use regular NSObjects withCore Data
    79. 79. NSManagedObject- Generic class that implements all basic behaviorrequired by CoreData model object- It’s not possible to use regular NSObjects withCore DataCan be created using following ways:
    80. 80. NSManagedObject- Generic class that implements all basic behaviorrequired by CoreData model object- It’s not possible to use regular NSObjects withCore DataCan be created using following ways:1. Designated initializer
    81. 81. NSManagedObject- Generic class that implements all basic behaviorrequired by CoreData model object- It’s not possible to use regular NSObjects withCore DataCan be created using following ways:1. Designated initializer- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context
    82. 82. NSManagedObject- Generic class that implements all basic behaviorrequired by CoreData model object- It’s not possible to use regular NSObjects withCore DataCan be created using following ways:1. Designated initializer- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context2. Static method of NSEntityDescription class
    83. 83. NSManagedObject- Generic class that implements all basic behaviorrequired by CoreData model object- It’s not possible to use regular NSObjects withCore DataCan be created using following ways:1. Designated initializer- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context2. Static method of NSEntityDescription class+ (id)insertNewObjectForEntityForName:(NSString *)entityName inManagedObjectContext:(NSManagedObjectContext *)context
    84. 84. Create NSManagedObjects to represent your data model NSManagedObject NSManagedObject NSManagedObject
    85. 85. NSManagedObjects are held in NSManagedObjectContext’s cacheNSManagedObject NSManagedObject NSManagedObject
    86. 86. NSManagedObjects are held in NSManagedObjectContext’s cacheNSManagedObject NSManagedObject NSManagedObject
    87. 87. NSManagedObjects are held in NSManagedObjectContext’s cacheNSManagedObject NSManagedObject NSManagedObjectContext NSManagedObject
    88. 88. NSManagedObjects are held in NSManagedObjectContext’s cacheNSManagedObject NSManagedObject NSManagedObjectContext NSManagedObject
    89. 89. NSManagedObjects are held in NSManagedObjectContext’s cacheNSManagedObject NSManagedObject NSManagedObjectContext NSManagedObject Save
    90. 90. Demo
    91. 91. iCloud
    92. 92. Why should you use iCloud APIs?
    93. 93. Why should you use iCloud APIs?1. There’s no need to configure and maintain servers
    94. 94. Why should you use iCloud APIs?1. There’s no need to configure and maintain servers2. Syncs across all devices without your effort
    95. 95. Why should you use iCloud APIs?1. There’s no need to configure and maintain servers2. Syncs across all devices without your effort3. Autosaving and conflict resolving
    96. 96. Why should you use iCloud APIs?1. There’s no need to configure and maintain servers2. Syncs across all devices without your effort3. Autosaving and conflict resolving4. Easily revert to a previous version
    97. 97. iCloud
    98. 98. iCloud
    99. 99. iCloudDocument storage
    100. 100. iCloudDocument storage
    101. 101. iCloudDocument storage Key-value storage
    102. 102. Document storage Application sandbox
    103. 103. Document storageApplication sandbox
    104. 104. Document storageApplication iCloud-monitored sandbox directory
    105. 105. Document storageApplication iCloud-monitored NSFileManager sandbox directory
    106. 106. Document storageApplication iCloud-monitored NSFileManager sandbox directory
    107. 107. Document storage setUbiquitous:YESApplication iCloud-monitored NSFileManager sandbox directory
    108. 108. Document storage setUbiquitous:YESApplication iCloud-monitored NSFileManager sandbox directory
    109. 109. Document storage setUbiquitous:YESApplication iCloud-monitored NSFileManager sandbox directory setUbiquitous:NO
    110. 110. Limitations
    111. 111. Limitations1. Does not work on simulator
    112. 112. Limitations1. Does not work on simulator2. A provisioning profile and entitlements are needed to test
    113. 113. Limitations1. Does not work on simulator2. A provisioning profile and entitlements are needed to test3. All files stored in iCloud must be managed by an object thatimplements NSFilePresenter protocol
    114. 114. Limitations1. Does not work on simulator2. A provisioning profile and entitlements are needed to test3. All files stored in iCloud must be managed by an object thatimplements NSFilePresenter protocolUIKit provides UIDocument class which implementsNSFilePresenter protocol
    115. 115. Be responsible!
    116. 116. Be responsible!1. Don’t store any auto generated files in iCloud.
    117. 117. Be responsible!1. Don’t store any auto generated files in iCloud.2. Don’t keep any copies of files tracked by iCloud.
    118. 118. 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.
    119. 119. 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.
    120. 120. Demo
    121. 121. Where to go from here?
    122. 122. Where to go from here?
    123. 123. Where to go from here?http://www.raywenderlich.com/934/core-data-tutorial-getting-started
    124. 124. Where to go from here?http://www.raywenderlich.com/934/core-data-tutorial-getting-started https://developer.apple.com/videos/wwdc/2011/
    125. 125. 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
    126. 126. Thank you!https://github.com/Tuszy/UserDefaultsSamplehttps://github.com/Tuszy/NSCacheSamplehttps://github.com/Tuszy/ArchivesSamplehttps://github.com/Tuszy/CoreDataSamplehttps://github.com/Tuszy/iCloudSample @srgtuszy srgtuszy@gmail.com srgtuszy.github.com
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×