Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Everyday CoreData
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Pat Zearfoss
March 16, 2016
Technology
0
83
Everyday CoreData
My CoreData talk at CodersOnly on March 16, 2016
Pat Zearfoss
March 16, 2016
Tweet
Share
More Decks by Pat Zearfoss
See All by Pat Zearfoss
CircleBack and WWDC 2015
pzearfoss
0
34
Other Decks in Technology
See All in Technology
コスト削減から「セキュリティと利便性」を担うプラットフォームへ
sansantech
PRO
3
1.4k
OWASP Top 10:2025 リリースと 少しの日本語化にまつわる裏話
okdt
PRO
3
710
IaaS/SaaS管理における SREの実践 - SRE Kaigi 2026
bbqallstars
4
2.2k
Claude_CodeでSEOを最適化する_AI_Ops_Community_Vol.2__マーケティングx_AIはここまで進化した.pdf
riku_423
2
550
生成AI時代にこそ求められるSRE / SRE for Gen AI era
ymotongpoo
5
3.1k
Azure Durable Functions で作った NL2SQL Agent の精度向上に取り組んだ話/jat08
thara0402
0
180
Frontier Agents (Kiro autonomous agent / AWS Security Agent / AWS DevOps Agent) の紹介
msysh
3
170
GitLab Duo Agent Platform × AGENTS.md で実現するSpec-Driven Development / GitLab Duo Agent Platform × AGENTS.md
n11sh1
0
130
CDKで始めるTypeScript開発のススメ
tsukuboshi
1
390
10Xにおける品質保証活動の全体像と改善 #no_more_wait_for_test
nihonbuson
PRO
2
240
StrandsとNeptuneを使ってナレッジグラフを構築する
yakumo
1
110
Introduction to Sansan for Engineers / エンジニア向け会社紹介
sansan33
PRO
6
68k
Featured
See All Featured
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Become a Pro
speakerdeck
PRO
31
5.8k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
1
120
Making the Leap to Tech Lead
cromwellryan
135
9.7k
Code Review Best Practice
trishagee
74
20k
GitHub's CSS Performance
jonrohan
1032
470k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
140
Evolving SEO for Evolving Search Engines
ryanjones
0
120
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
64
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
190
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
200
RailsConf 2023
tenderlove
30
1.3k
Transcript
Everyday CoreData Pat Zearfoss
About Me • Pat Zearfoss • CircleBack on the app
store • Running on a CoreData stack for better than a year. • @patzearfoss on Twitter.
End User License Agreement • My suggestions may not be
100% right for all situations • CoreData elicits a lot of opinions in the community. • Your app may not fit the same patterns and models
Lets Go
Things I’ll talk about • Frameworks and tools • Model
design • App architecture • Testing • Debugging tools
CoreData Review
CoreData Review • A way of persisting data in Cocoa
applications • Not a database • Well, it kind of is, but not necessarily • On label usage is “object graph persistence”
Object Graph
CoreData Pieces • NSManagedObjectModel • NSPesistentStoreCoordinator • NSManagedObjectContext • NSManagedObject
Tip #1 Once you understand CoreData, use a framework (or
write one) to make life easier.
There’s tons • Magical Record • BNRCoreData • RestKit
Why • It’ll help you simplify all the stuff you’ll
do anyway. • CoreData can be . . . wordy.
Creating a new instance if let eDesc = NSEntityDescription.entityForName(“Contact”, inManagedObjectContext:
context) { let contact1 = Contact(entity: contactDescription, insertIntoManagedObjectContext:context) }
A basic fetch let fetchRequest = NSFetchRequest(entityName: “Contact”) fetchRequest.sortDescriptors =
[NSSortDescriptor(key: “lastName”, ascending: true)] fetchRequest.predicate = NSPredicate(format: “isFavorite = YES”) if let context = context { do { try contacts = context. executeFetchRequest(fetchRequest) as! [Contact] } catch { print (“couldn’t load contacts”) } }
The app delegate eyebleed
Creating an instance - MR if let contact1 = Contact.MR_createEntityInContext(context)
Fetch if let fetchedContacts = Contact.MR_findAllSortedBy(“lastName”, ascending: true, inContext: context) as? [Contact]
Eyebleed cured MagicalRecord.setupCoreDataStack()
Demo 1
More tools • SimPholders • CoreDataEditor • SQLPro or other
Sqlite tool
Modeling • Your managed model is a database, but it
also isn’t. You have to make it work right for you.
Tip #2 Design for your app. Don’t feel the need
to appease the database gods.
Demo 2
Tip #3 Use NSManagedObjectContexts liberally • Creating a new context
to fetch objects, passing the ids to a main context for use on the UI. • Doing expensive operations in background queues • Creating a throw-away editing context.
Demo 3
Tip #4 NSFetchedResultsController can be your best friend • NSFetchedResultsController
manages the results of a fetch into a context.
NSFetchedResultsController • Keeping a UI updated with changes from the
internet. • Use fetched results controllers to manage all communication with a web backend.
Demo 4
Architectural patterns from fetched results controllers
None
Testing • You don’t really care about persistence • You
just want to test this object, controller, view model, etc.
Tip #5 Use an in-memory store for testing • Sets
up the same scheme you have in memory. • Quick and easy to set up an tear down between tests
Demo 5
Debugging
Tip #6 Learn and use the CoreData environment variables
Useful environment variables • com.apple.CoreData.SQLDebug [1,2,3] • com.apple.CoreData.SyntaxColoredLogging 1 •
com.apple.CoreData.ConcurrencyDebug 1 • (not ThreadDebug) • com.apple.CoreData.MigrationDebug
Demo 6
Tip #7 Use the CoreData tool in instruments
Demo 7 CircleBack App
Tip #8 Other people have really good ideas • Saul
Mora - original author of MagicalRecord • Marcus Zarra - wrote many a book on CoreData • objc.io on CoreData • WWDC Material (obvi)
Recap • Find a good framework to help you. •
Design a model that fits your app’s needs • Make use of NSManagedObjectContext • Use NSFetchedResultsControllers
Recap • Use in memory stores for testing • CoreData
runtime arguments are your friend • Make use of Instruments • Read good material
?s and Thanks! •
[email protected]
• @patzearfoss