Skip to content
This repository was archived by the owner on Aug 18, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: determines if migration needs to occur
  • Loading branch information
heyjaywilson committed Dec 30, 2022
commit 2e6e1f62cc5537528dbf90686e492b89a78a33a1
12 changes: 5 additions & 7 deletions brain-marks/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@
import SwiftUI

struct ContentView: View {
let migrationService: MigrationService
let migrationService: MigrationService = MigrationService()

@State private var showAddSheet = false

init(storageProvider: StorageProvider) {
self.migrationService = MigrationService(managedObjectContext: storageProvider.context)
}

var body: some View {
CategoryList()
.onAppear {
migrationService.performMigration()
if migrationService.checkIfMigrationShouldRun() {
migrationService.performMigration()
}
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(storageProvider: StorageProvider())
ContentView()
}
}
22 changes: 14 additions & 8 deletions brain-marks/Services/MigrationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ import Foundation

/// Controls the migration from Amplify to CoreData
class MigrationService {
/// Need to get category from Amplify
/// Store Category inside CD
/// Loop through category and get tweets
/// Store tweet in CD and associate to proper category
private let storageProvider = StorageProvider.shared
private var awsCategories: [AWSCategory] = []

private let amplifyDataStore = DataStoreManger.shared

private var managedObjectContext: NSManagedObjectContext

init(managedObjectContext: NSManagedObjectContext) {
self.managedObjectContext = managedObjectContext
init() {
self.managedObjectContext = storageProvider.context
}

func performMigration() {
Expand All @@ -38,12 +35,21 @@ class MigrationService {
}
}
do {
try managedObjectContext.save()
try managedObjectContext.save()
} catch {
print("❌ MigrationService.performMigration() Error: \(error)")
}
}
UserDefaults.standard.set(true, forKey: "migrationToCoreDataRan")
}

public func checkIfMigrationShouldRun() -> Bool {
let migrationToCoreDataRan = UserDefaults.standard.bool(forKey: "migrationToCoreDataRan")

if storageProvider.getAllCategories().isEmpty && !migrationToCoreDataRan {
return true
}
return false
}

private func getCategories() {
Expand All @@ -52,7 +58,7 @@ class MigrationService {
case .success(let categories):
self.awsCategories = categories
case .failure(let error):
print("MigrationService.getCategories(): Error: \(error)")
print("MigrationService.getCategories(): Error: \(error)")
}
})
}
Expand Down
8 changes: 4 additions & 4 deletions brain-marks/brain_marksApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import TelemetryClient

@main
struct brain_marksApp: App {

@AppStorage("migrationToCoreDataRan")
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 adding this variable here do, when it isn't used?

Copy link
Author

Choose a reason for hiding this comment

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

the purpose of this being here is to run before the migration service does on first launch. It'll default to false on the first app run since being introduced. It's then set to true to not run the migration again.

private var migrationToCoreDataRan: Bool = false

init() {
configureAmplify()

Expand All @@ -21,11 +23,9 @@ struct brain_marksApp: App {
TelemetryManager.send(TelemetrySignals.appLaunchedRegularly)
}

let storageProvider = StorageProvider.shared

var body: some Scene {
WindowGroup {
ContentView(storageProvider: storageProvider)
ContentView()
}
}

Expand Down