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.
What is the purpose of doing a refreshObject immediately after the fetch? This table contains just a few rows. Is this necessary? Any downside to removing all together?
context.performBlockAndWait {
    let className = self.className()
    let fetchRequest = NSFetchRequest(entityName: className)
    groups = (try? context.executeFetchRequest(fetchRequest)) as? [FavoriteLocationGroup]
    if groups != nil {
        groups!.sortInPlace { $0.name < $1.name }
        for group in groups! {
            context.refreshObject(group, mergeChanges: false) <<< crashes here
        }
    }
}
Thanks in advance for any help you can provide!


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
    }
    ...
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With