Justin Domnitz
Justin Domnitz

Reputation: 3307

EXC_BAD_ACCESS in NSManagedObjectContext refreshObject

I've inherited some swift code which performs a refreshObject immediately after fetching from core data. The objects being fetched have a relationship to another table. The fetch and refreshObject are inside a performBlockAndWait. The code is below. The crash appeared to begin in iOS9.

Thanks in advance for any help you can provide!

value of context

what group is

Upvotes: 5

Views: 918

Answers (1)

Zmey
Zmey

Reputation: 2558

Could you try replacing sortInPlace line with

groups = groups!.sort { $0.name < $1.name }

I've hit similar issue. The app was crashing in Release mode in random place after calling sortInPlace. It seems that it causes some kind of memory corruption. sort() works well.

Upd: I was able to reproduce the crash with the following code (also crashes in OS X Cocoa apps):

import Cocoa

class MyObject {
    var x: Int?
}

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!

    var array = [MyObject]()

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application

        for x in 0..<100000 {
            let object = MyObject()
            object.x = x
            array.append(object)
        }
        array.sortInPlace { $0.x < $1.x } // CRASH
    }
    ...
}

Upvotes: 6

Related Questions