HomeiOS Developmentios - App crashes on CoreData deletion...tips on how to code this...

ios – App crashes on CoreData deletion…tips on how to code this accurately?


I’ve 2 core knowledge entity’s arrange with a many to at least one and one to many relationship between them (Present -> Season one to many / Season -> Present many to at least one). I’ve the category for Present manually created with the code under. Nonetheless, after I delete a present or season (if just one), it’s crashing the app saying ‘Deadly error: Unexpectedly discovered nil whereas unwrapping an Non-obligatory worth’ for the next code within the handbook class, which was setup to create the connection between the 2 entities. I’ve the app arrange to make use of a for loop to delete all seasons of a present previous to deleting the present as effectively, which causes the identical error clearly as a result of there are not any seasons left. How do I repair this in order that it deletes all seasons with out an error?

**Reveals+CoreDataProperties.swift (handbook class for Present Entity)
**

@NSManaged public var season: Set<Seasons>?

    public var seasons: [Seasons]{
            let setOfSeasons = season
            return setOfSeasons!.sorted{
                $0.id > $1.id
            }
        }

Deletion code inside view

    // DELETE SHOW
    public func deleteShow(present: Reveals){
        deleteSeasonsinShow(present: present)
        viewContext.delete(present)
        do{
            attempt viewContext.save()
        } catch{
            print(error)
        }
    }
    
    // DELETE SEASONS WITHIN SHOW PRIOR TO DELETING SHOW
    public func deleteSeasonsinShow(present: Reveals){
        
        for season in present.seasons {
            viewContext.delete(season)
            do{
                attempt viewContext.save()
            } catch{
                print(error)
            }
        }
    }

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments