3lvis / DATAStack

100% Swift Simple Boilerplate Free Core Data Stack. NSPersistentContainer
Other
214 stars 44 forks source link

Crash DATAStack.backgroundContextaDidSave #111

Closed hei9gag closed 6 years ago

hei9gag commented 6 years ago

Hi, I got some crashes when using DATAStack and Instabug together. It seems the crash related to DATAStack listen to all NSManagedObjectContext notification thrown by other parties. And cause bad memory address. Do you have any ideas how to fix it? Many thanks.

screen shot 2018-01-24 at 10 02 18 am

3lvis commented 6 years ago

Hi @hei9gag,

I'm not sure what's going on here. DATAStack doesn't listen to all the notifications. It listens to notifications for the NSManagedContext that's been saved.

For example here

    @objc public lazy var mainContext: NSManagedObjectContext = {
        let context = //...
        //....

        NotificationCenter.default.addObserver(self, selector: #selector(DATAStack.mainContextDidSave(_:)), name: .NSManagedObjectContextDidSave, object: context)

        return context
    }()

If you see in NotificationCenter.default.addObserver we pass the context as the object. This means that only notifications for this context will be received.

https://developer.apple.com/documentation/foundation/nsnotificationcenter/1415360-addobserver?language=objc

screen shot 2018-01-25 at 10 36 05

Not sure what Instabug is doing internally.

hei9gag commented 6 years ago

Thanks @3lvis I have also checked the code and the notification part looks good to me too. I will check with Instabug again.

3lvis commented 6 years ago

No problem, have a nice day and good luck!

3lvis commented 6 years ago

This might be related https://github.com/3lvis/DATAStack/pull/112

hei9gag commented 6 years ago

Thanks it fixed the crash problem. Learn something new again.