3
votes

Please explain the below crash..

#3. NSManagedObjectContext 0x1701c9ab0: xxxxxxxxxx 0 libsystem_kernel.dylib 0x1848898e8 __ulock_wait + 8 1 libdispatch.dylib 0x18475977c _dispatch_ulock_wait + 48 2 libdispatch.dylib 0x1847598a4 _dispatch_thread_event_wait_slow + 36 3 libdispatch.dylib 0x1847574f0 _dispatch_barrier_sync_f_slow + 236 4 CoreData 0x187c0703c _perform + 232 5 CoreData 0x187c178b8 -[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:] + 176 6 CoreData 0x187b675e0 -[NSManagedObjectContext executeFetchRequest:error:] + 580 7 MyApp 0x1002f596c specialized static RecentItemsController.cleanupItems(inContext:ofType:limitingTo:sortingByAttribute:withPredicate:) (xxxxx.swift:182) 8 MyApp 0x1002f6574 specialized closure #1 in static xxx.xxx(inStore:completion:) (xxx.swift:163) 9 MyApp 0x1002f5118 partial apply for closure #1 in static xxx.xxx(inStore:completion:) (xxx.swift) 10 CoreData 0x187c0c214 developerSubmittedBlockToNSManagedObjectContextPerform + 152 11 libdispatch.dylib 0x1847469a0 _dispatch_client_callout + 16 12 libdispatch.dylib 0x184754ad4 _dispatch_queue_serial_drain + 928 13 libdispatch.dylib 0x18474a2cc _dispatch_queue_invoke + 884 14 libdispatch.dylib 0x184754fa8 _dispatch_queue_override_invoke + 344 15 libdispatch.dylib 0x184756a50 _dispatch_root_queue_drain + 540 16 libdispatch.dylib 0x1847567d0 _dispatch_worker_thread3 + 124 17 libsystem_pthread.dylib 0x18494f100 _pthread_wqthread + 1096 18 libsystem_pthread.dylib 0x18494ecac start_wqthread + 4

1
If you look toward the end of those lines you'll see RecentItemsController.swift:182. That's where the crash happened, in that file at line 182. That's the code you need to look at.Tom Harrington
Why not line number 163?user804417
It says that the code on line 163 called the code on line 182. Line 163 may be involved but line 182 is where the crash happened.Tom Harrington
How to interpret that line number 163 called line number 182 & not the vice versa?user804417
Because that's how stack trace ordering works. Also if you look at those two lines you can probably tell which one calls the other.Tom Harrington

1 Answers

1
votes

Looking at the stack trace and the code inside cleanupItems func, I believe the problem is in the force unwrapping code:

let itemsToDelete = try context.fetch(request) as! [R]

The first thing to write a crash preventive code is below

if let itemsToDelete = try context.fetch(request) as? [R] {
   itemsToDelete.forEach(context.delete(_:))
   log("Deleted \(itemsToDelete.count) \(type) item(s)")

}

Let me know if that works.