GrahamDennis / GDCoreDataConcurrencyDebugging

GDCoreDataConcurrencyDebugging helps you find cases where NSManagedObject's are being called on the wrong thread or dispatch queue.
MIT License
170 stars 23 forks source link

Confused about an error #23

Open orion538 opened 8 years ago

orion538 commented 8 years ago

I'm getting an error using the following code:

let context: NSManagedObjectContext = self.getPrivateContext()

context.performBlockAndWait {
        if let key = self.appDelegate.APIKey {
            apiKey = key
        } else {
            if let key: String = self.appDelegate.MainCoreDataService.getApiKeyFromDatabase(context) where key != "" {
                apiKey = key
                self.appDelegate.APIKey = key
            }
        }
}

internal func getApiKeyFromDatabase(context: NSManagedObjectContext) -> String? {
    let fetchRequest: NSFetchRequest = NSFetchRequest()
    if let entity: NSEntityDescription = NSEntityDescription.entityForName(Constants.CoreData.Device.NameOfTable, inManagedObjectContext: context) {
        // Edit the entity name as appropriate.
        fetchRequest.entity = entity

        do {
            guard let devices: [Device] = try context.executeFetchRequest(fetchRequest) as? [Device]
                where devices.count == 1
                else {
                    Constants.Log.error("Apikey not found in database (unexpected result!)")
                    return nil
            }
            let key = devices[0].apiKey
            return key
        } catch {
            Constants.Log.error("Apikey not found in database (unexpected result!)")
        }
    }
    return nil
}

I'm quite lost. It's telling me about the release of an object not being correct. I'm lost. Do you maybe know what i could do to fix this? I'm using Swift.

Invalid concurrent access to managed object calling 'release'; Stacktrace: ( 0 x App 0x00549a71 ValidateConcurrency + 228 1 x App 0x005497b7 CustomSubclassRelease + 18 2 x App 0x00265ad4 _TFC8x_App6DevicecfMS0_FT6entityCSo19NSEntityDescription30insertIntoManagedObjectContextGSqCSo22NSManagedObjectContextS0_ + 264 3 x App 0x00265bc8 _TToFC8x_App6DevicecfMS0_FT6entityCSo19NSEntityDescription30insertIntoManagedObjectContextGSqCSo22NSManagedObjectContextS0_ + 100 4 CoreData 0x220bbf2b + 154 5 CoreData 0x220bad27 + 158 6 CoreData 0x220cc2d1 + 396 7 CoreData 0x22144527 + 766 8 CoreData 0x22144a99 + 324 9 CoreData 0x22146db3 + 66 10 libdispatch.dylib 0x01dc3f51 _dispatch_barrier_sync_f_slow_invoke + 564 11 libdispatch.dylib 0x01db6c83 _dispatch_client_callout + 22 12 libdispatch.dylib 0x01dbb76d _dispatch_main_queue_callback_4CF + 1680 13 CoreFoundation 0x209773fd + 8 14 CoreFoundation 0x209758f7 + 1574 15 CoreFoundation 0x208c8bf9 CFRunLoopRunSpecific + 520 16 CoreFoundation 0x208c89e5 CFRunLoopRunInMode + 108 17 GraphicsServices 0x21b14ac9 GSEventRunModal + 160 18 UIKit 0x24b58ba1 UIApplicationMain + 144 19 x App 0x00344ed4 main + 164 20 libdyld.dylib 0x20577873 + 2 )

GrahamDennis commented 8 years ago

I can't figure out what code lines frames 2 and 3 refer to. I'd recommend putting a breakpoint in ValidateConcurrency for when it fails to try to figure out exactly which lines this stacktrace corresponds to.