I'm doing a multithreaded CoreBluetooth app where the iPhone is working as a central, scanning (based on UI-interaction) for peripherals which then are connected and processed, resulting in various changes to the app's CoreData model used to store persistent info on the state of the different peripherals that the user has access to (without having to connected to them every time the user starts the app).
The user basically request connection to a peripheral via the UI, which then kick an async worker thread that will try to accomplish the request within a certain time-frame (e.g., a few minutes), else reporting a failure/timeout back to the UI.
Everything is working fine, but I just now started to add support for background execution since it's very likely that the users will start the async worked thread that will start to scan for BLE-peripherals, and then lock the phone and place in their pockets, expecting things to work when the peripheral is within reach.
However, Bluetooth-wise things work as they should (peripheral is discovered, connected, and processed), but I suddenly get CoreData exceptions;
CoreData: error: exception during newFetchedPKsForSourceID: I/O error for database at /var/mobile/Applications/8640B48D-8744-436A-A083-C39F63FBFB3F/Documents/Model.sqlite. SQLite error code:266, 'not an error' errno:1 with userInfo of {
NSFilePath = "/var/mobile/Applications/8640B48D-8744-436A-A083-C39F63FBFB3F/Documents/Model.sqlite";
NSPOSIXErrorDomain = 1;
NSSQLiteErrorDomain = 266;
}
CoreData: error: exception during newFetchedPKsForSourceID: authorization denied with userInfo of {
NSFilePath = "/var/mobile/Applications/8640B48D-8744-436A-A083-C39F63FBFB3F/Documents/Model.sqlite";
NSSQLiteErrorDomain = 23;
}
CoreData: error: exception during newFetchedPKsForSourceID: authorization denied with userInfo of {
NSFilePath = "/var/mobile/Applications/8640B48D-8744-436A-A083-C39F63FBFB3F/Documents/Model.sqlite";
NSSQLiteErrorDomain = 23;
}
CoreData: error: exception during newFetchedPKsForSourceID: authorization denied with userInfo of {
NSFilePath = "/var/mobile/Applications/8640B48D-8744-436A-A083-C39F63FBFB3F/Documents/Model.sqlite";
NSSQLiteErrorDomain = 23;
}
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'I/O error for database at /var/mobile/Applications/8640B48D-8744-436A-A083-C39F63FBFB3F/Documents/Model.sqlite. SQLite error code:266, 'not an error' errno:1'
So, in short; all's good as long as I don't put the app in background execution mode, which to me indicate that threading and sync/merge/CoreData issues are properly setup. But something changes when the app goes into background...
Looking forward to your thoughts and ideas,
/Markus