iOS 10. Coredata Insert New Object Sig Abrt

iOS 10. CoreData insert new object sig ABRT

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

What's the possible cause of this Core Data crash

Finally, I think I've fixed the crash though I'm not sure whether it is my bug or Core Data's bug.

First of all, it has nothing to do with the sync among MOCs.

Secondly, yes, initWithEntity:insertIntoManagedObjectContext: is called when objects are first instantiated in moc, whether they are newly created or fetched with a request. So it seem the doc is misleading:

If context is not nil, this method invokes [context insertObject:self]
(which causes awakeFromInsert to be invoked).

If it is called for fetching, awakeFromFetch is invoked.

As you can see in the backtrace, the crash happened in my overridden initWithEntity:insertIntoManagedObjectContext:. Reasonable enough, initWithEntity:insertIntoManagedObjectContext: turned out to be the cause of the crash. But I don't think I did anything bad in it. I just used some persistent attributes to initialize some non-persistent attributes:

- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context {
self = [super initWithEntity:entity insertIntoManagedObjectContext:context];
if (self) {
// xxx_ is a persistent attribute while _xxx is the corresponding non-persistent attribute as its cache in memory only to reduce reading cost.
_xxx = self.xxx_.unsignedIntegerValue;
}
return self;
}

Sometimes when I read the first persistent attribute the unfulfillable exception was thrown up.

I always know that

You are discouraged from overriding this method—you should instead
override awakeFromInsert and/or awakeFromFetch (if there is logic
common to these methods, it should be factored into a third method
which is invoked from both). If you do perform custom initialization
in this method, you may cause problems with undo and redo operations.

But I don't know it would cause unfulfillable exceptions. Is it a Core Data bug? Otherwise, what's wrong with my code?

IOS 10 core data executeFetchRequest crashes if called before insertNewObjectForEntityForName

Thanks for the help.

It seems to be a static method that gets called twice which re-initialized a variable.

Found the solution while looking for the crash log, and stumbled over Zombie in xcode...

Core Data Relationship data not being saved

You will first need to insert the ent2 in the same way as ent1

 let ent2 = NSEntityDescription.insertNewObjectForEntityForName("Ent2".....

ent2.info = "Info"

then,

 ent1.ent2 = ent2

than call the save

Predicate search in core data causes SIGABRT on second time through. NSBetweenPredicateOperatorType

here ya go...

BETWEEN operations are aggregate operations, and aggregate operations are not supported by Core Data. You'll need to turn this into:

NSPredicate *p = [NSPredicate predicateWithFormat:@"%K >= %@ AND %K <= %@", keyPath, lowerBound, keyPath, upperBound];


Related Topics



Leave a reply



Submit