Slide 1

Slide 1 text

Architecture Design for Local Database ~ Realm, CoreData, SwiftData ~ Musa Yazici

Slide 2

Slide 2 text

Have you ever struggled with a local database?

Slide 3

Slide 3 text

Database objects come with so many rules!

Slide 4

Slide 4 text

For Example: Realm This Realm object cannot be retrieved or modified like a normal variable.

Slide 5

Slide 5 text

● Realm instance handling ● Use identical thread ● etc. Rules for Realm

Slide 6

Slide 6 text

There are so many rules for local database objects! For Realm 1. Crashes when accessing an object across threads 2. Crashes when editing a realm object outside of a realm.write {} block 3. Crashes when trying to access a property that is not in the model definition 4. 4. Crashes when trying to access a deleted realm object 5. 5. Crashes due to resource leakage or invalid state reference if Realm instance is not properly closed For CoreData 1. Crashes when Context is shared between different Threads 2. Crashes when trying to access a result set before a fetch request completes 3. Crashes when trying to access an entity that is not in the model definition 4. Crashes when manipulating a Managed Object that has been deleted or does not belong to a context. 5. Crashes when attempting to set data of an unexpected type for a model attribute 6. Crashes when compiled model does not match model in code

Slide 7

Slide 7 text

There are so many rules for local database objects! For Realm 1. Crashes when accessing an object across threads 2. Crashes when editing a realm object outside of a realm.write {} block 3. Crashes when trying to access a property that is not in the model definition 4. 4. Crashes when trying to access a deleted realm object 5. 5. Crash due to resource leakage or invalid state reference if Realm instance is not properly closed For CoreData 1. Crashes when Context is shared between different Threads 2. Crashes when trying to access a result set before a fetch request completes 3. Crashes when trying to access an entity that is not in the model definition 4. Crashes when manipulating a Managed Object that has been deleted or does not belong to a context. 5. Crash when attempting to set data of an unexpected type for a model attribute 6. Crashes when compiled model does not match model in code It’s hard to always take care of rules

Slide 8

Slide 8 text

Even if rules are violated, there are no compile errors and it’s difficult to find the violation in the code. It may not be found until someone crashes.

Slide 9

Slide 9 text

Database object rules are dangerous for both Quality and Efficiency

Slide 10

Slide 10 text

We want to use database objects without rules!

Slide 11

Slide 11 text

We can use Dummy Objects!

Slide 12

Slide 12 text

1.Prepare a database object and a normal object Dummy Object Realm Object

Slide 13

Slide 13 text

2. Allow objects to be converted from one to another Dummy Object Realm Object

Slide 14

Slide 14 text

3. Make service class for database operations Use non-Realm data in entire project

Slide 15

Slide 15 text

4. Fetch database objects after launch and convert them to normal object Fetch RealmCards and convert to Card Array

Slide 16

Slide 16 text

When the cards array is changed, synchronizeWithRealm() is called Convert Card to RealmCard and apply the data changes in background thread 5. Update database objects automatically whenever normal objects are updated

Slide 17

Slide 17 text

Normal Project Realm Card

Slide 18

Slide 18 text

All files that use RealmCard (View, ViewModel, Test, etc) Need to obey Realm rules Realm Card Normal Project

Slide 19

Slide 19 text

Using dummy object Fetch RealmCard after launch. Convert to Card Change Realm data whenever Card is changed Realm Card Card Realm Service

Slide 20

Slide 20 text

Using dummy object Fetch RealmCard after launch. Convert to Card Change Realm data whenever Card is changed Realm Card Card Realm Service Entire app uses non-Realm Card class. All the files except for RealmService are independent on Realm. Development can be done without thinking about Realm!

Slide 21

Slide 21 text

Benefits of using dummy objects 1. Improvement in maintainability and development efficiency Become independent of Database! Develop without thinking about it! 2. Great UI performance Data operations are executed in background thread. Local database can be slow with large amount of data, but it’s always fast with this architecture. 3. Easy to write tests Test execution is independent of database. Parallel execution can be enabled without effort. 4. Independence of the code You can migrate database by rewriting just a small part of your project.

Slide 22

Slide 22 text

Database objects are dangerous it's better to avoid touching them directly

Slide 23

Slide 23 text

Thank you!