swiftcore-dataios10xcode8-beta2

iOS 10. CoreData insert new object sig ABRT


I tried forEntityName: "Game", "MyApp.Game".

in my dataManagerFile:

let appDelegate = UIApplication.shared().delegate as! AppDelegate
                        let container = appDelegate.persistentContainer

                        let managedObjectContext = container.viewContext
                        for item in items {
                            let word = NSEntityDescription.insertNewObject(forEntityName: "MyApp.Game", into: managedObjectContext) as! Game

in Game+coreDataProperties file

extension Game {

    @nonobjc class func fetchRequest() -> NSFetchRequest<Game> {
        return NSFetchRequest<Game>(entityName: "Game");
    }

in Game+coreDataClass

class Game: NSManagedObject {

}

I generated files for CoreData using .xcdatamodeld: in object inspector I specified Name: "Game", Class "Game", Module "Current Product Module", codegen "class definition", then I used Editor/create subclass, after I got 3 files I set codegen property to "Manual/None"

I got run-time error: "signal SIGABRT" when trying to cast to Game in:

NSEntityDescription.insertNewObject(forEntityName: "MyApp.Game", into: managedObjectContext) as! Game

What am I doing wrong? Also could you advice me a good manual on CoreData, with latest changes applied?


Solution

  • Finally, I have made it to work.
    This isn't the first issue I have faced while working with CoreData in iOS 10, on xCode Beta.
    So here is sequence of steps for adding your entities in xCode 8:
    1.When creating your app pick CoreData option, this will create youAppName.xcdatamodeld file
    2. choice youAppName.xcdatamodeld, add Entity button
    3. add required attributes
    4. use data Model inspector to setup code generation:
    Entity Name "Game", Class Name "GameEntity", Module "Current Product Module", codegen "class definition"
    5. cmd + s
    6. Editor/create NSManagedObjectSubclass
    7. You will get 3 new files:
    GameEntity+CoreDataClass
    GameEntity+CoreProperties
    COREDATA
    8. Comment out content of the file "COREDATA..."
    9. youAppName.xcdatamodeld, data Model inspector, now when you have generated files, change Codeine property from "class definition" to "Manual/None"

    Here is a code sample on how to add new objects:

    let appDelegate = UIApplication.shared().delegate as! AppDelegate
                            let container = appDelegate.persistentContainer
    
                            let managedObjectContext = container.viewContext
                            for item in items {
                                let game = NSEntityDescription.insertNewObject(forEntityName: "Game", into: managedObjectContext) as! GameEntity
                                game.status = item.status
                                game.rs = item.rs
                                game.score = item.score
    
    
                                do {
                                    try managedObjectContext.save()
    

    here is a code sample on how to remove data:

    let appDelegate = UIApplication.shared().delegate as! AppDelegate
            let container = appDelegate.persistentContainer
            let managedObjectContext = container.viewContext
            let fetchRequest: NSFetchRequest<GameEntity> = GameEntity.fetchRequest()
            do {
                let games = try managedObjectContext.fetch(fetchRequest) 
                for game in games {
                    managedObjectContext.delete(game)
                }