iOS 10. CoreData insert new object sig ABRT

时光总嘲笑我的痴心妄想 提交于 2019-12-07 18:34:57

问题


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?


回答1:


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.




回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/38427366/ios-10-coredata-insert-new-object-sig-abrt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!