SlideShare a Scribd company logo
Qardio experience with
Core Data.
Can a fancy stack solve all your problems?
Dmitrii Ivanov
iOS Team Lead
at Qardio
or
In this talk
1. Qardio iOS app

2. Common Core Data issues we faced

3. Our struggle (part I)

4. Questions to ask yourself before diving into Core Data

5. Core Data stacks

6. Our struggle (part II)

7. Results and conclusion
part 1 of 7
part 1 of 7
Data synchronisation in
Qardio
• Off-line work with locally stored data

• Data from our bluetooth devices

• Bidirectional exchange with the cloud

• Bidirectional exchange with HealthKit
Currently: 40 Core Data entities, 50 data model versions
part 1 of 7
Common Core Data issues
• Multithreading violation

• Context merge issues

• Deadlocks and UI freezes

• Versioning and migration

• Crashes... and more crashes
part 2 of 7
Multithreading violation
part 2 of 7
Collection was mutated while
being enumerated
Multithreading violation
part 2 of 7
CoreData couldn’t fulfil a
fault
Multithreading violation
part 2 of 7
• Multithreading violation

• Context merge issues

• Deadlocks and UI freezes

• Versioning and migration

• Crashes... and more crashes
Common Core Data issues
part 2 of 7
version 3 version 3version 3a version 3b
new version: 4
Versioning and migration
part 2 of 7
• Multithreading violation

• Context merge issues

• Deadlocks and UI freezes

• Versioning and migration

• Crashes... and more crashes
Common Core Data issues
part 2 of 7
Fatal Exception:
NSInternalInconsistencyException
when saving the context
Crashes
part 2 of 7
Our struggle (part I)
Versioning and migrations
• Start with versioning from the beginning

• New version per production release if there are
any changes

• Remember about migrations from the previous
versions
part 3 of 7
Synchronisation through
the Main context
Main context abuse
part 3 of 7
Issues with UI performance
Wrong Stack
is the root of all evil
Questions to ask before
integrating Core Data
• Do I need Core Data?

• Which Core Data benefits can we utilise?

• How deeply incapsulated should it be?

• Which stack to use?

• How to properly use the stack?

• Why do I need versioning?

• Which rules to follow when writing new code?
part 4 of 7
What is Core Data?
Not ORM, not SQLite wrapper 

but an object graph
MVC, MVVM, MVP (VIPER)
part 4 of 7
Do I need Core Data in my
project?
Not necessarily
part 4 of 7
Core Data essence
•Object relations 

•Object lifecycle (insertion, mutation,
deletion)

•Data access optimisations (faulting, row
cache)
part 4 of 7
FMDB - https://github.com/ccgus/fmdb > 12k stars
SQLite.swift - https://github.com/stephencelis/SQLite.swift > 5k stars
SQLiteManager4iOS - https://github.com/misato/SQLiteManager4iOS - 125 stars
SQift - https://github.com/Nike-Inc/SQift - 39 stars
SQLite wrappers
YapDatabase - https://github.com/yapstudios/YapDatabase > 3k stars
Disk - https://github.com/saoudrizwan/Disk > 2k stars
Couchbase - https://github.com/couchbase/couchbase-lite-ios > 1,3k stars
LevelDB (C++) - https://github.com/google/leveldb > 14k stars
Other options
part 4 of 7
How deeply should it be
integrated?
Abstraction level
Deep
Integration
Complete
Abstraction
part 4 of 7
Model:
NSManagedObject
UI
Business
Logic
Data
Layer
Deep Integration
part 4 of 7
Abstraction level
Deep
Integration
Relative
Abstraction
•Easy to use, no extra code

•All the benefits: relations,
lifecycle, optimisations

•Inspired by Apple
Complete
Abstraction
part 4 of 7
Model:
NSManagedObject
UI
Business
Logic
Data
Layer
Model
(plain object/struct)
Relative Abstraction
part 4 of 7
Abstraction level
Deep
Integration
Relative
Abstraction
•Architectural cleanness an
lose coupling

•Less overhead with
multithreading

•Less direct access to the
coordinator and the store
Complete
Abstraction
•Easy to use, no extra code

•All the benefits: relations,
lifecycle, optimisations

•Inspired by Apple
part 4 of 7
What Core Data stack 

to choose?
What is Core Data Stack?
NSManagedObjectContext (main, private)
NSPersistentStoreCoordinator
Store (SQLite, XML, Binary, In-memory)
part 5 of 7
Simple
Pros:
• Simplicity 

• Data base interaction is fast
Cons:
• No background operations
part 5 of 7
Nested 1
(PSC - Main - Worker)
Pros:
• Automatic syncing the Worker context

• Disposable Workers
Cons:
• I/O to the store in the main thread (for
all the contexts)
part 5 of 7
Nested 2
(PSC - Worker - Main)
Pros:
• I/O in the background
Cons:
• Manual syncing the Main context

• Busy Worker blocks the store
from the Main context
part 5 of 7
Nested Context
Pros:
• Disposable Workers

• Automatic merging Workers to the
Main context

• I/O on a Private context
Cons:
• Pushing the changes down to the store

• Passing all the Workers’ changes through
the Main context

• Significant UI impact on high-load
operations

• No merge policy between the contexts
part 5 of 7
Shared Coordinator
Pros:
• Independent communication to the
coordinator

• Common row cache

• Adjustable merge policy

• Flexible context merge
Cons:
• Manual setup of the merge policy

• Merge is not instant

• The policy to solve deleted objects
conflicts
part 5 of 7
Shared Store
Pros:
• Minimal interaction between the
contexts

• Ability to work with the store from
completely different locations
Cons:
• No common row cache

• Accessing the disc when merging
changes

• No ObjectID (just URIRepresentation)
part 5 of 7
Mixed
Pros:
• Background I/O to the store

• Disposable Workers

• Maximum synchronisation
between the Workers and the
Main context

• Separate stack for high-load
operations
Cons:
• Complex set up and tuning

• Additional complexity in using
part 5 of 7
Functional comparison
part 5 of 7
Efficiency comparison
How to choose the stack?
• Pick the least complex one

• Consider your data flow and use cases

a. which data is being changed from UI, which - from the background

b. is there any high-load operations?

c. is there a lot of simultaneous high-load operations and UI data
access?

d. how possible the conflicts in data changes are?

e. how important the entry threshold to the stack is

• It’s all about your priorities
part 5 of 7
Functional comparison
part 5 of 7
Our struggle (part II)
Qardio: migration to
Mixed Stack
BNRCoreDataStack
• Easy async initialising with
just one function

• Binding context with
notifications observing

• Pushing the changes down to
the store

• Handy extensions for Core
Data classes
part 6 of 7
Improvements caused by
the migration
• Code revision

• Proper contexts for different tasks (Main,
Worker, Batch Worker)

• Merge policy set up
part 6 of 7
Some issues were solved 

(no merge conflicts, less crashes and UI
problems)
New issues started to appear
Developers
are the root of all evil
Further improvements
• Knowledge sharing inside the team

• Abstracting some models from Core Data

• Multithreading sanitation
part 6 of 7
Abstracting some models from Core Data
part 6 of 7
Abstracting some models from Core Data
part 6 of 7
Further improvements
• Knowledge sharing inside the team

• Abstracting some models from Core Data

• Multithreading sanitation
part 6 of 7
Multithreading sanitation
part 6 of 7
“All that is left to us is honor!”
part 6 of 7
General Rules
• Understand your edge of framework abstraction

• Don’t access NSManagedObjects out of their
context

• Remember that NSManagedObject is not
NSObject

• Keep in mind versioning and migrations
part 7 of 7
•Easy to use, no extra code

•All the benefits: relations,
lifecycle, optimisations

•Inspired by Apple
Abstraction level
Deep
Integration
Relative
Abstraction
•Architectural cleanness an
lose coupling

•Less overhead with
multithreading

•Less direct access to the
coordinator and the store
Complete
Abstraction
We are
somewhere
here
part 7 of 7
General Rules
• Understand your edge of framework abstraction

• Don’t access NSManagedObjects out of their
context

• Remember that NSManagedObject is not
NSObject

• Keep in mind versioning and migrations
part 7 of 7
Mixed Stack Usage Rules
•Only UI related operations in
the Main MOC

•Limited operations in the
Regular Workers

•All the rest should go to the
Batch Worker
part 7 of 7
Results
• UI issues disappeared

• Context merge issues disappeared

• Migration and versioning problems disappeared

• Multithreading violations (almost) disappeared

• Amount of crashes dramatically decreased (less
than 1 crash/day instead of dozens)
part 7 of 7
Conclusions
• Core Data may bring you much pain

• Deep understanding of the framework before writing
production code is essential

• Share knowledge inside the team

• Stack is not a silver bullet (although quite important)

• Create clear rules and principles for writing code

• Enforce the rules with any possible tools
• Core Data may bring you much pain

• Deep understanding of the framework before writing production code is essential

• Share knowledge inside the team

• Stack is not a silver bullet (although quite important)

• Create clear rules and principles for writing code

• Enforce the rules with any possible tools
part 7 of 7
telegram: @topolog
e-mail: dmtopolog@gmail.com

More Related Content

Similar to Qardio experience with Core Data

Why retail companies can't afford database downtime
Why retail companies can't afford database downtimeWhy retail companies can't afford database downtime
Why retail companies can't afford database downtime
DBmaestro - Database DevOps
 
Microservices: The Best Practices
Microservices: The Best PracticesMicroservices: The Best Practices
Microservices: The Best Practices
Pavel Mička
 
Performant Django - Ara Anjargolian
Performant Django - Ara AnjargolianPerformant Django - Ara Anjargolian
Performant Django - Ara Anjargolian
Hakka Labs
 
Debugging,Troubleshooting & Monitoring Distributed Web & Cloud Applications a...
Debugging,Troubleshooting & Monitoring Distributed Web & Cloud Applications a...Debugging,Troubleshooting & Monitoring Distributed Web & Cloud Applications a...
Debugging,Troubleshooting & Monitoring Distributed Web & Cloud Applications a...
Theo Jungeblut
 
Software Architecture and Architectors: useless VS valuable
Software Architecture and Architectors: useless VS valuableSoftware Architecture and Architectors: useless VS valuable
Software Architecture and Architectors: useless VS valuable
Comsysto Reply GmbH
 
Clean architecture with asp.net core
Clean architecture with asp.net coreClean architecture with asp.net core
Clean architecture with asp.net core
Sam Nasr, MCSA, MVP
 
Architectural Decisions: Smoothly and Consistently
Architectural Decisions: Smoothly and ConsistentlyArchitectural Decisions: Smoothly and Consistently
Architectural Decisions: Smoothly and Consistently
Comsysto Reply GmbH
 
Architectural Decisions: Smoothly and Consistently
Architectural Decisions: Smoothly and ConsistentlyArchitectural Decisions: Smoothly and Consistently
Architectural Decisions: Smoothly and Consistently
Comsysto Reply GmbH
 
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
mfrancis
 
The challenges and pitfalls of database deployment automation
The challenges and pitfalls of database deployment automationThe challenges and pitfalls of database deployment automation
The challenges and pitfalls of database deployment automation
DBmaestro - Database DevOps
 
Nine Neins - where Java EE will never take you
Nine Neins - where Java EE will never take youNine Neins - where Java EE will never take you
Nine Neins - where Java EE will never take you
Markus Eisele
 
In (database) automation we trust
In (database) automation we trustIn (database) automation we trust
In (database) automation we trust
DBmaestro - Database DevOps
 
Java Spring
Java SpringJava Spring
Java Spring
AathikaJava
 
50 Shades of Fail KScope16
50 Shades of Fail KScope1650 Shades of Fail KScope16
50 Shades of Fail KScope16
Christian Berg
 
Architecting modern Android apps
Architecting modern Android appsArchitecting modern Android apps
Architecting modern Android apps
Grigori Hlopkov
 
20141206 4 q14_dataconference_i_am_your_db
20141206 4 q14_dataconference_i_am_your_db20141206 4 q14_dataconference_i_am_your_db
20141206 4 q14_dataconference_i_am_your_db
hyeongchae lee
 
Framework Enabling End-Users to Maintain Web Applications (ICICWS2015)
Framework Enabling End-Users to Maintain Web Applications (ICICWS2015)Framework Enabling End-Users to Maintain Web Applications (ICICWS2015)
Framework Enabling End-Users to Maintain Web Applications (ICICWS2015)
Masayuki Nii
 
All about that reactive ui
All about that reactive uiAll about that reactive ui
All about that reactive ui
Paul van Zyl
 
A Note on Distributed Computing - Papers We Love Hyderabad
A Note on Distributed Computing - Papers We Love HyderabadA Note on Distributed Computing - Papers We Love Hyderabad
A Note on Distributed Computing - Papers We Love Hyderabad
Hrishikesh Barua
 
DBmaestro's State of the Database Continuous Delivery Survey- Findings Revealed
DBmaestro's State of the Database Continuous Delivery Survey- Findings RevealedDBmaestro's State of the Database Continuous Delivery Survey- Findings Revealed
DBmaestro's State of the Database Continuous Delivery Survey- Findings Revealed
DBmaestro - Database DevOps
 

Similar to Qardio experience with Core Data (20)

Why retail companies can't afford database downtime
Why retail companies can't afford database downtimeWhy retail companies can't afford database downtime
Why retail companies can't afford database downtime
 
Microservices: The Best Practices
Microservices: The Best PracticesMicroservices: The Best Practices
Microservices: The Best Practices
 
Performant Django - Ara Anjargolian
Performant Django - Ara AnjargolianPerformant Django - Ara Anjargolian
Performant Django - Ara Anjargolian
 
Debugging,Troubleshooting & Monitoring Distributed Web & Cloud Applications a...
Debugging,Troubleshooting & Monitoring Distributed Web & Cloud Applications a...Debugging,Troubleshooting & Monitoring Distributed Web & Cloud Applications a...
Debugging,Troubleshooting & Monitoring Distributed Web & Cloud Applications a...
 
Software Architecture and Architectors: useless VS valuable
Software Architecture and Architectors: useless VS valuableSoftware Architecture and Architectors: useless VS valuable
Software Architecture and Architectors: useless VS valuable
 
Clean architecture with asp.net core
Clean architecture with asp.net coreClean architecture with asp.net core
Clean architecture with asp.net core
 
Architectural Decisions: Smoothly and Consistently
Architectural Decisions: Smoothly and ConsistentlyArchitectural Decisions: Smoothly and Consistently
Architectural Decisions: Smoothly and Consistently
 
Architectural Decisions: Smoothly and Consistently
Architectural Decisions: Smoothly and ConsistentlyArchitectural Decisions: Smoothly and Consistently
Architectural Decisions: Smoothly and Consistently
 
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
 
The challenges and pitfalls of database deployment automation
The challenges and pitfalls of database deployment automationThe challenges and pitfalls of database deployment automation
The challenges and pitfalls of database deployment automation
 
Nine Neins - where Java EE will never take you
Nine Neins - where Java EE will never take youNine Neins - where Java EE will never take you
Nine Neins - where Java EE will never take you
 
In (database) automation we trust
In (database) automation we trustIn (database) automation we trust
In (database) automation we trust
 
Java Spring
Java SpringJava Spring
Java Spring
 
50 Shades of Fail KScope16
50 Shades of Fail KScope1650 Shades of Fail KScope16
50 Shades of Fail KScope16
 
Architecting modern Android apps
Architecting modern Android appsArchitecting modern Android apps
Architecting modern Android apps
 
20141206 4 q14_dataconference_i_am_your_db
20141206 4 q14_dataconference_i_am_your_db20141206 4 q14_dataconference_i_am_your_db
20141206 4 q14_dataconference_i_am_your_db
 
Framework Enabling End-Users to Maintain Web Applications (ICICWS2015)
Framework Enabling End-Users to Maintain Web Applications (ICICWS2015)Framework Enabling End-Users to Maintain Web Applications (ICICWS2015)
Framework Enabling End-Users to Maintain Web Applications (ICICWS2015)
 
All about that reactive ui
All about that reactive uiAll about that reactive ui
All about that reactive ui
 
A Note on Distributed Computing - Papers We Love Hyderabad
A Note on Distributed Computing - Papers We Love HyderabadA Note on Distributed Computing - Papers We Love Hyderabad
A Note on Distributed Computing - Papers We Love Hyderabad
 
DBmaestro's State of the Database Continuous Delivery Survey- Findings Revealed
DBmaestro's State of the Database Continuous Delivery Survey- Findings RevealedDBmaestro's State of the Database Continuous Delivery Survey- Findings Revealed
DBmaestro's State of the Database Continuous Delivery Survey- Findings Revealed
 

Recently uploaded

A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
kalichargn70th171
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
ISH Technologies
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Paul Brebner
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
Yara Milbes
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLESINTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
anfaltahir1010
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
XfilesPro
 
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
Tier1 app
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
gapen1
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 

Recently uploaded (20)

A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLESINTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
 
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 

Qardio experience with Core Data

  • 1. Qardio experience with Core Data. Can a fancy stack solve all your problems? Dmitrii Ivanov iOS Team Lead at Qardio or
  • 2.
  • 3. In this talk 1. Qardio iOS app 2. Common Core Data issues we faced 3. Our struggle (part I) 4. Questions to ask yourself before diving into Core Data 5. Core Data stacks 6. Our struggle (part II) 7. Results and conclusion
  • 6. Data synchronisation in Qardio • Off-line work with locally stored data • Data from our bluetooth devices • Bidirectional exchange with the cloud • Bidirectional exchange with HealthKit Currently: 40 Core Data entities, 50 data model versions part 1 of 7
  • 7. Common Core Data issues • Multithreading violation • Context merge issues • Deadlocks and UI freezes • Versioning and migration • Crashes... and more crashes part 2 of 7
  • 9. Collection was mutated while being enumerated Multithreading violation part 2 of 7
  • 10. CoreData couldn’t fulfil a fault Multithreading violation part 2 of 7
  • 11. • Multithreading violation • Context merge issues • Deadlocks and UI freezes • Versioning and migration • Crashes... and more crashes Common Core Data issues part 2 of 7
  • 12. version 3 version 3version 3a version 3b new version: 4 Versioning and migration part 2 of 7
  • 13. • Multithreading violation • Context merge issues • Deadlocks and UI freezes • Versioning and migration • Crashes... and more crashes Common Core Data issues part 2 of 7
  • 16. Versioning and migrations • Start with versioning from the beginning • New version per production release if there are any changes • Remember about migrations from the previous versions part 3 of 7
  • 19. Issues with UI performance
  • 20. Wrong Stack is the root of all evil
  • 21. Questions to ask before integrating Core Data • Do I need Core Data? • Which Core Data benefits can we utilise? • How deeply incapsulated should it be? • Which stack to use? • How to properly use the stack? • Why do I need versioning? • Which rules to follow when writing new code? part 4 of 7
  • 22. What is Core Data? Not ORM, not SQLite wrapper but an object graph MVC, MVVM, MVP (VIPER) part 4 of 7
  • 23. Do I need Core Data in my project? Not necessarily part 4 of 7
  • 24. Core Data essence •Object relations •Object lifecycle (insertion, mutation, deletion) •Data access optimisations (faulting, row cache) part 4 of 7
  • 25. FMDB - https://github.com/ccgus/fmdb > 12k stars SQLite.swift - https://github.com/stephencelis/SQLite.swift > 5k stars SQLiteManager4iOS - https://github.com/misato/SQLiteManager4iOS - 125 stars SQift - https://github.com/Nike-Inc/SQift - 39 stars SQLite wrappers YapDatabase - https://github.com/yapstudios/YapDatabase > 3k stars Disk - https://github.com/saoudrizwan/Disk > 2k stars Couchbase - https://github.com/couchbase/couchbase-lite-ios > 1,3k stars LevelDB (C++) - https://github.com/google/leveldb > 14k stars Other options part 4 of 7
  • 26. How deeply should it be integrated?
  • 29. Abstraction level Deep Integration Relative Abstraction •Easy to use, no extra code •All the benefits: relations, lifecycle, optimisations •Inspired by Apple Complete Abstraction part 4 of 7
  • 31. Abstraction level Deep Integration Relative Abstraction •Architectural cleanness an lose coupling •Less overhead with multithreading •Less direct access to the coordinator and the store Complete Abstraction •Easy to use, no extra code •All the benefits: relations, lifecycle, optimisations •Inspired by Apple part 4 of 7
  • 32. What Core Data stack to choose?
  • 33. What is Core Data Stack? NSManagedObjectContext (main, private) NSPersistentStoreCoordinator Store (SQLite, XML, Binary, In-memory) part 5 of 7
  • 34. Simple Pros: • Simplicity • Data base interaction is fast Cons: • No background operations part 5 of 7
  • 35. Nested 1 (PSC - Main - Worker) Pros: • Automatic syncing the Worker context • Disposable Workers Cons: • I/O to the store in the main thread (for all the contexts) part 5 of 7
  • 36. Nested 2 (PSC - Worker - Main) Pros: • I/O in the background Cons: • Manual syncing the Main context • Busy Worker blocks the store from the Main context part 5 of 7
  • 37. Nested Context Pros: • Disposable Workers • Automatic merging Workers to the Main context • I/O on a Private context Cons: • Pushing the changes down to the store • Passing all the Workers’ changes through the Main context • Significant UI impact on high-load operations • No merge policy between the contexts part 5 of 7
  • 38. Shared Coordinator Pros: • Independent communication to the coordinator • Common row cache • Adjustable merge policy • Flexible context merge Cons: • Manual setup of the merge policy • Merge is not instant • The policy to solve deleted objects conflicts part 5 of 7
  • 39. Shared Store Pros: • Minimal interaction between the contexts • Ability to work with the store from completely different locations Cons: • No common row cache • Accessing the disc when merging changes • No ObjectID (just URIRepresentation) part 5 of 7
  • 40. Mixed Pros: • Background I/O to the store • Disposable Workers • Maximum synchronisation between the Workers and the Main context • Separate stack for high-load operations Cons: • Complex set up and tuning • Additional complexity in using part 5 of 7
  • 43. How to choose the stack? • Pick the least complex one • Consider your data flow and use cases a. which data is being changed from UI, which - from the background b. is there any high-load operations? c. is there a lot of simultaneous high-load operations and UI data access? d. how possible the conflicts in data changes are? e. how important the entry threshold to the stack is • It’s all about your priorities part 5 of 7
  • 46. Qardio: migration to Mixed Stack BNRCoreDataStack • Easy async initialising with just one function • Binding context with notifications observing • Pushing the changes down to the store • Handy extensions for Core Data classes part 6 of 7
  • 47. Improvements caused by the migration • Code revision • Proper contexts for different tasks (Main, Worker, Batch Worker) • Merge policy set up part 6 of 7
  • 48. Some issues were solved (no merge conflicts, less crashes and UI problems) New issues started to appear
  • 49. Developers are the root of all evil
  • 50. Further improvements • Knowledge sharing inside the team • Abstracting some models from Core Data • Multithreading sanitation part 6 of 7
  • 51. Abstracting some models from Core Data part 6 of 7
  • 52. Abstracting some models from Core Data part 6 of 7
  • 53. Further improvements • Knowledge sharing inside the team • Abstracting some models from Core Data • Multithreading sanitation part 6 of 7
  • 55. “All that is left to us is honor!” part 6 of 7
  • 56. General Rules • Understand your edge of framework abstraction • Don’t access NSManagedObjects out of their context • Remember that NSManagedObject is not NSObject • Keep in mind versioning and migrations part 7 of 7
  • 57. •Easy to use, no extra code •All the benefits: relations, lifecycle, optimisations •Inspired by Apple Abstraction level Deep Integration Relative Abstraction •Architectural cleanness an lose coupling •Less overhead with multithreading •Less direct access to the coordinator and the store Complete Abstraction We are somewhere here part 7 of 7
  • 58. General Rules • Understand your edge of framework abstraction • Don’t access NSManagedObjects out of their context • Remember that NSManagedObject is not NSObject • Keep in mind versioning and migrations part 7 of 7
  • 59. Mixed Stack Usage Rules •Only UI related operations in the Main MOC •Limited operations in the Regular Workers •All the rest should go to the Batch Worker part 7 of 7
  • 60. Results • UI issues disappeared • Context merge issues disappeared • Migration and versioning problems disappeared • Multithreading violations (almost) disappeared • Amount of crashes dramatically decreased (less than 1 crash/day instead of dozens) part 7 of 7
  • 61. Conclusions • Core Data may bring you much pain • Deep understanding of the framework before writing production code is essential • Share knowledge inside the team • Stack is not a silver bullet (although quite important) • Create clear rules and principles for writing code • Enforce the rules with any possible tools • Core Data may bring you much pain • Deep understanding of the framework before writing production code is essential • Share knowledge inside the team • Stack is not a silver bullet (although quite important) • Create clear rules and principles for writing code • Enforce the rules with any possible tools part 7 of 7 telegram: @topolog e-mail: dmtopolog@gmail.com