mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-11 20:00:30 +01:00
Application Crashes when accessing attribute of a record #197
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @ujamshy on GitHub (Feb 5, 2018).
Hi, I need your help with a problem that causes our application to crash.
The crash is happening at customers` devices - we couldn't reproduce it on our tests
Architecture:
The relevant table in the database contains log records.
LogManager contains a DataStack and uses it to create/insert/update/delete log records.
LogObserver uses ListMonitor to get notifications about changes to the table and communicates these changes to a remote server.
It runs in its own serial queue and uses un UnsafeTransaction created from the DataStack to handle managed objects.
When it gets a notification about a new/updated object (didInsertObject/didUpdateObject) it performs the following code:
LogObserver's transaction is created in its own queue the first time it is used:
When handling an object we try to check if it is deleted by testing if its context is nil:
Crash:
The crash happens sometimes when a record is deleted from the DataStack while still held by the LogObserver.
When printing the record all its attributes are displayed as nil (although they are not optionals).
When trying to access one of the attributes the application crashes with Exception Type: SIGTRAP.
Questions:
I have several questions:
Thanks :)
@JohnEstropia commented on GitHub (Feb 8, 2018):
CoreStore's transactions are designed more for updating data than observing for updates. There will be a hiccup with your DataStack-to-Transaction synchronization because of the changes propagating in different queues. My suggestion here would be to try using a
ListMonitorcreated from theUnsafeDataTransactionand observe that directly.That said, I haven't personally used a
ListMonitorcreated from anUnsafeDataTransactionin my projects (although I know some users who had) so I cannot offer advices against existing pitfalls, if there are any.If you have the luxury of redesigning your architecture, I strongly recommend you reduce the granularity of your transactions and to use
perform(asynchronous:...)andperform(synchronous:)when you can. There is a reason that CoreStore's Async and Sync transactions execute serially: they protect you from synchronization issues like these by not having to worry about changes in parallel transactions.@ujamshy commented on GitHub (Feb 8, 2018):
Let me refine my question. Suppose we have DataStack and UnsafeTransaction derived from the DataStack. Suppose also that an object in the DataStack and was added to the unsafe transaction using featchExisting. If the object is deleted from the DataStack should accessing it in the UnsafeTransaction cause exception?
@JohnEstropia commented on GitHub (Feb 15, 2018):
@ujamshy That's more of Core Data's behavior than anything, and yes that may trigger an exception.
@JohnEstropia commented on GitHub (Apr 14, 2018):
I'm closing this issue as this is Core Data's behavior. Let me know if you find a resolution so we can put a warning somewhere in the README.