简体   繁体   中英

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?

First, no one has updated the documentation for the changes in Core Data for iOS 10 yet. Expect them to start showing up in the next couple of months (Apple included).

Second, with the new changes to Core Data, you should no longer need to cast on creation. Ideally you can now just do:

let word = Game(context: managedObjectContext)

And it will do the correct thing.

BTW, You should not need to call the entity "MyApp.Game". It should be just "Game" and that is probably the core of your error.

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)
            }

First of all, Xcode 8 automatically generates the classes needed to represent the entities, so you don't have to create them manually. Instead of:

    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

You could try this:

    let appDelegate = UIApplication.shared().delegate as! AppDelegate
    let container = appDelegate.persistentContainer
    let managedObjectContext = container.viewContext
    for item in items {
        let word = NSEntityDescription.insertNewObject(forEntityName: "Game", into: managedObjectContext) as! Game

But make sure to give the proper name to the class in Data Model Inspector Setting class name in Data Model Inspector

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM