Skip to content
This repository was archived by the owner on Aug 18, 2023. It is now read-only.

Conversation

@heyjaywilson
Copy link

@heyjaywilson heyjaywilson commented Dec 31, 2022

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.

  • CategoryEntity holds the categories and has a one to many relationship to the TweetEntity.
  • TweetEntity holds the tweets and has a many to one relationship to the CategoryEntity.

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

@heyjaywilson heyjaywilson changed the base branch from dev to mikaela/bugfix-data December 31, 2022 04:23
@heyjaywilson heyjaywilson marked this pull request as ready for review December 31, 2022 04:45
@heyjaywilson heyjaywilson changed the title Jay/bugfix data Core Data integration Dec 31, 2022
Copy link
Owner

@mikaelacaron mikaelacaron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll chat in Discord 👍

init(inMemory: Bool = false) {
container = NSPersistentContainer(name: "BrainMarks")

if inMemory {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this do?
Also considering this is a singleton, the init is supposed to be private, because you aren't supposed to have multiple instances of it at a time. Would this affect what the init does?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make the init private, but this tells the app which provider to look at for tests and such.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any unit tests for this? I didn't see any @heyjaywilson

@mikaelacaron mikaelacaron self-requested a review February 10, 2023 00:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants