mikaelacaron / brain-marks

[Not Active] Open-source iOS app to save and categorize tweets
MIT License
80 stars 47 forks source link

Core Data integration #180

Open heyjaywilson opened 1 year ago

heyjaywilson commented 1 year ago

Progress / Closed on issue #

What it Does

TLDR: Introduces Core Data as the main data store 💽

📙 I left some more explanations in comments in the code cause that's easier for future maintainers to see.

Adds Core Data for Tweet and Category CRUD

Core Data has been added and the supporting files can be found in the CoreData folder.

There are two entities.

CoreData/StorageProvider.swift is used to initialize the NSPersistentContainer. It has preview variable which is used for the SwiftUI canvas/preview and a shared variable which is used all other times.

Migration from Amplify to Core Data

How a migration is performed:

  1. On app launch, a check is done if the migration needs to happen.
  2. All categories are fetched from AWS Amplify
  3. For each category:
    1. Translate the AWS Category to a CoreDataEntity
    2. Fetch the AWS Tweets from the AWS Category
    3. For each tweet, translate it to a TweetEntity and add it to the CategoryEntity tweets
  4. Save to core data

How is a Migration determined:

The following conditions must both be true for a migration to run.

  1. storageProvider.getAllCategories().isEmpty is true
  2. The user default migrationToCoreDataRan is true

How I Tested

  1. Run the application from dev branch, create a category and save some tweets
  2. Run the application from jay/bugfix-data and see the categories look the same (migration ran)
  3. Save some more tweets and the tweets persist through app load
  4. Edit the category name and see it stays the same through app loads