Your SlideShare is downloading. ×
Data perisistence in iOS
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Data perisistence in iOS

1,625
views

Published on

Michał Tuszyński (@srgtuszy) talk from Cracow.mobi

Michał Tuszyński (@srgtuszy) talk from Cracow.mobi

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,625
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
71
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
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

    • 1. Data Persistence in iOS Michał Tuszyński iOS/Android developer at appvetica @srgtuszy srgtuszy.github.com
    • 2. Roadmap
    • 3. Roadmap1. NSUserDefaults
    • 4. Roadmap1. NSUserDefaults2. NSCache
    • 5. Roadmap1. NSUserDefaults2. NSCache3. NSCoding protocol
    • 6. Roadmap1. NSUserDefaults2. NSCache3. NSCoding protocol4. CoreData
    • 7. Roadmap1. NSUserDefaults2. NSCache3. NSCoding protocol4. CoreData5. iCloud
    • 8. What areNSUserDefaults?
    • 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 data2. Heavy objects
    • 13. What should not be stored in NSUserDefaults?1. Sensitive data2. Heavy objects3. 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!
    • 17. Why 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!
    • 29. What are 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. Archives
    • 33. Archives
    • 34. ArchivesKeyed archives
    • 35. ArchivesKeyed archives
    • 36. ArchivesKeyed archives Sequential archives
    • 37. What is the difference?
    • 38. What is the difference?
    • 39. What is the difference?Sequential archives
    • 40. What is the difference? Sequential archives-Encoded from root objectthrough all interrelated objects
    • 41. What is the difference? Sequential archives-Encoded from root objectthrough all interrelated objects-Must be decoded in the sameorder it was encoded
    • 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. 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. 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. 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. How to create an archive out of a custom object?
    • 47. How to create an archive out of a custom object? 1.Implement NSCoding protocol inside the object
    • 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. 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. Demo
    • 51. Core Data
    • 52. CoreData is an efficient framework to manage your object persistence
    • 53. CoreData is an efficient framework to manage your object persistenceMain features:
    • 54. CoreData is an efficient framework to manage your object persistenceMain features:-Change tracking and undo support
    • 55. CoreData is an efficient framework to manage your object persistenceMain features:-Change tracking and undo support-Relationship between entities maintenance
    • 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. 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. What Core Data is not?
    • 59. What Core Data is not?-It is not a relational database
    • 60. What Core Data is not?-It is not a relational database-It’s not a silver bullet for your code
    • 61. How does it work?
    • 62. How does it work? Storage
    • 63. How does it work? Storage
    • 64. How does it work? Storage NSPersistentStoreCoordinator
    • 65. How does it work? Storage The bridge between the storage NSPersistentStoreCoordinator and the application
    • 66. How does it work? Storage The bridge between the storage NSPersistentStoreCoordinator and the application
    • 67. How does it work? StorageNSManagedObjectModel The bridge between the storage NSPersistentStoreCoordinator and the application
    • 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. 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. 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. 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. NSManagedObjectModel
    • 73. NSManagedObjectModel
    • 74. NSManagedObjectModel Entity
    • 75. NSManagedObjectModel Entity Relationship
    • 76. NSManagedObject
    • 77. NSManagedObject- Generic class that implements all basic behaviorrequired by CoreData model object
    • 78. NSManagedObject- Generic class that implements all basic behaviorrequired by CoreData model object- It’s not possible to use regular NSObjects withCore Data
    • 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. 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. 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. 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. 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. Create NSManagedObjects to represent your data model NSManagedObject NSManagedObject NSManagedObject
    • 85. NSManagedObjects are held in NSManagedObjectContext’s cacheNSManagedObject NSManagedObject NSManagedObject
    • 86. NSManagedObjects are held in NSManagedObjectContext’s cacheNSManagedObject NSManagedObject NSManagedObject
    • 87. NSManagedObjects are held in NSManagedObjectContext’s cacheNSManagedObject NSManagedObject NSManagedObjectContext NSManagedObject
    • 88. NSManagedObjects are held in NSManagedObjectContext’s cacheNSManagedObject NSManagedObject NSManagedObjectContext NSManagedObject
    • 89. NSManagedObjects are held in NSManagedObjectContext’s cacheNSManagedObject NSManagedObject NSManagedObjectContext NSManagedObject Save
    • 90. Demo
    • 91. iCloud
    • 92. Why should you use iCloud APIs?
    • 93. Why should you use iCloud APIs?1. There’s no need to configure and maintain servers
    • 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. 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. 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. iCloud
    • 98. iCloud
    • 99. iCloudDocument storage
    • 100. iCloudDocument storage
    • 101. iCloudDocument storage Key-value storage
    • 102. Document storage Application sandbox
    • 103. Document storageApplication sandbox
    • 104. Document storageApplication iCloud-monitored sandbox directory
    • 105. Document storageApplication iCloud-monitored NSFileManager sandbox directory
    • 106. Document storageApplication iCloud-monitored NSFileManager sandbox directory
    • 107. Document storage setUbiquitous:YESApplication iCloud-monitored NSFileManager sandbox directory
    • 108. Document storage setUbiquitous:YESApplication iCloud-monitored NSFileManager sandbox directory
    • 109. Document storage setUbiquitous:YESApplication iCloud-monitored NSFileManager sandbox directory setUbiquitous:NO
    • 110. Limitations
    • 111. Limitations1. Does not work on simulator
    • 112. Limitations1. Does not work on simulator2. A provisioning profile and entitlements are needed to test
    • 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. 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. Be responsible!
    • 116. Be responsible!1. Don’t store any auto generated files in iCloud.
    • 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. 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. 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. Demo
    • 121. Where to go from here?
    • 122. Where to go from here?
    • 123. Where to go from here?http://www.raywenderlich.com/934/core-data-tutorial-getting-started
    • 124. Where to go from here?http://www.raywenderlich.com/934/core-data-tutorial-getting-started https://developer.apple.com/videos/wwdc/2011/
    • 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. 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

    ×