barnabus
barnabus

Reputation: 882

Core Data - executeFetchRequest Swift 1.2 Release mode crashes

After upgrading to Swift 1.2 in Xcode 6.3 Beta, all my code that carried out a fetchRequest now causes and EXC_BAD_ACCESS

Here is an example, where I delete the entities based on the entity name:

func deleteAllEntities(entityName: String)
{
    println(entityName)
    var error: NSError? = nil
    let allEntityFetchRequest = NSFetchRequest(entityName: entityName)
    if let savedObjects = self.managedObjectContext?.executeFetchRequest(allEntityFetchRequest, error: &error) as? [NSManagedObject]
    {
        for object in savedObjects
        {
            self.managedObjectContext?.deleteObject(object as NSManagedObject)
        }
        // save changes persistent store
        if !(self.managedObjectContext!.save(&error))
        {
            println("ERROR: Error saving model: \(error?.localizedDescription)")
        }
    }
    else
    {
        println("ERROR: Fetch error: \(error!.localizedDescription)")
    }
}

Another example is:

var error: NSError? = nil
if let venueObjects = self.managedObjectContext?.executeFetchRequest(relatedVenuesEntityFetchRequest, error: &error) as? [NSManagedObject]
{
// do stuff
}

I don't see any differences between Swift 1.1 and 1.2 in the release notes with respect to this. Any thoughts? The Fetch request returns [AnyObject]!, however if I try and run these fetch requests on specific entity types it still occurs.

Exact reason for crash:

objc release exc bad access

Crash is happening on line "for object in savedObjects". Debugger isn't offer much info.

enter image description here

Upvotes: 3

Views: 2047

Answers (2)

apetrov
apetrov

Reputation: 472

As you probably know:

Xcode does not generate Swift classes accurately when they have optional attributes. You must manually add the ? for optional values

It's not very clear from your question are your entities included in any 'to one' relations. But if that is the case, follow the upper suggestion.

Source: http://www.jessesquires.com/better-coredata-models-in-swift/

Upvotes: 2

barnabus
barnabus

Reputation: 882

I have narrowed the issue down to a compiler optimisation bug. Release mode defaults to 'Fastest [-O]'. Turning this to 'None -[Onone], resolves the issue.

enter image description here

Since this cannot be fixed by anyone but Apple, I have marked this as the answer. I have raised a Radar - 19843889.

Upvotes: 5

Related Questions