'Core Data rare crash "Dispatch queue: SQLQueue 0x1..."
I have an odd crash in my app that's difficult to reproduce. I did run into some threading issues before which seem to be mostly fixed now but every now and then I receive a crash log, even on my own devices.
My code fetches Librarys descending by the last time a book was added. For each of these libraries I want to see the most recently added books. The code works (except for the crash) but I can't think of a reason for the crash itself.
I apologize for the weird unwrapping at if let libraries1 = libraries0 { etc. but to me it seemed to have improved the situation which also may however only be imagination.
The last possible cause of issue that I may spot by myself is to declare libraries outside of the thread of the mainContext however I'm not sure if that can be the root of the problem since I would expect the crash to be way more present if it was a hard "don't".
Below you will see my code, the // CRASH: comment indicates the line that Xcode blames the crash on.
func readEntries( count: Int = 3, completion: @escaping ([Data], [String]) -> () ) {
var libraries = [Library]()
let fetchRequest = makeFetchRequest(LIBRARY)
let sort = NSSortDescriptor(key: "lastAddedTime", ascending: false)
fetchRequest.sortDescriptors = [sort]
fetchRequest.fetchLimit = count
mainContext.perform {
do {
let libraries0 = try self.mainContext.fetch(fetchRequest) as? [Library]
if let libraries1 = libraries0 {
libraries = libraries1
for lib in libraries {
if(lib.lastAddedTime > 0) {
let fetchRequest = self.makeFetchRequest(self.BOOK)
fetchRequest.fetchLimit = 5
let predicate = NSPredicate(format: "hostLibrary == %@", lib)
fetchRequest.predicate = predicate
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "time", ascending: false)]
if var books = try? self.mainContext.fetch(fetchRequest) as? [Book] { // CRASH: "Dispatch queue: SQLQueue 0x1020206e0 for DBBookModel.sqlite (0)"
print(books)
}
}
}
//completion ...
}
} catch let error as NSError {
print("Could not load. \(error), \(error.userInfo)")
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

