EXC_BAD_ACCESS in NSManagedObjectContext refreshObject

I inherited some quick code that executes refreshObject right after getting the kernel data. The selected objects are related to another table. The fetch and refreshObject object is inside executeBlockAndWait. The code is below. The crash seems to have started in iOS9.

  • Any idea why refreshObject is throwing EXC_BAD_ACCESS? What is a fix?
  • What is the purpose of creating a refreshObject immediately after extraction? This table contains just a few rows. It's necessary? Any flaw to remove everything 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!

context value

what a group

+4
source share
1 answer

sortInPlace

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

. Release sortInPlace. , . sort() .

: - ( OS X Cocoa ):

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
    }
    ...
}
+6

Source: https://habr.com/ru/post/1609859/


All Articles