skjiisa / Character-Tracker

Elder Scrolls character tracker for iOS
BSD 2-Clause "Simplified" License
1 stars 0 forks source link

Objects imported could have no Games and clutter the database #104

Closed skjiisa closed 3 years ago

skjiisa commented 3 years ago

If JSON is improperly formatted and does not contain any Games, it could be imported and loaded into the database without ever being able to access it.

skjiisa commented 3 years ago

Since I had Mods with no games in my install of the app, I simply threw this at the bottom of the preloadData() function in the app delegate to get all of the mods added to the Skyrims.

let context = CoreDataStack.shared.mainContext
let modsFR: NSFetchRequest<Mod> = Mod.fetchRequest()
let mods = try? context.fetch(modsFR)

let gamesFR: NSFetchRequest<Game> = Game.fetchRequest()
let allGames = try? context.fetch(gamesFR)
let skyrims = allGames?.filter { $0.name?.lowercased().contains("skyrim") ?? false }

for mod in mods ?? [] {
    for skyrim in skyrims ?? [] {
        mod.addToGames(skyrim)
    }
}
skjiisa commented 3 years ago

A cleanup script could run after imports to remove objects with no games. This could be done on the background thread the import happens on so that when the prompt shows the list of imported objects, invalid objects won't be included.