Core Data crash when try to fetch data

I received crash reports from Firebase Crashlytics and the same reports I see in XCode crashes tab, that application crashes in the first 2-5 seconds after launch when trying to fetch some data from DB. One interesting note that it's happens only on iOS 16.1.1, maybe who knows why it's happening? Here are a few parts of the code and stack trace:

final class MessagesViewController: UIViewController {
...
    var conversation: Conversation?
    private var _fetchedResultsController: NSFetchedResultsController<Message>?
    private var fetchedResultsController: NSFetchedResultsController<Message>? {
        if _fetchedResultsController == nil {
            _fetchedResultsController = createFetchedResultsController()
        }
        
        return _fetchedResultsController
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        performNewFetch()
        loadData()
    }

    private func performNewFetch() {
        self.fetchedResultsController?.fetchRequest.fetchLimit = 10
        
        do {
            try self.fetchedResultsController?.performFetch()
            self.tableView.reloadData()
        } catch {
            
        }
    }

    func loadData() {
        RestAPI.shared.fetchMessages() { pagination in
            ...
                self.fetchedResultsController?.fetchRequest.fetchLimit = pagination.offset + pagination.limit
            
                do {
                    try self.fetchedResultsController?.performFetch()
                    self.tableView.reloadData()
                } catch {
                
                }
            ...
        }
    }

    private func createFetchedResultsController() -> NSFetchedResultsController<Message>? {
        if let uuid = conversation?.uuid {
            let fetchRequest = NSFetchRequest<Message>(entityName: "Message")
            fetchRequest.sortDescriptors = [NSSortDescriptor(key: "createdAt", ascending: false)]
            fetchRequest.fetchLimit = 10
            fetchRequest.fetchOffset = 0
            fetchRequest.predicate = NSPredicate(format: "conversation.uuid == %@", uuid)
            
            let context = CoreDataManager.shared.managedObjectContext
            let fetchResultsController = NSFetchedResultsController(fetchRequest: fetchRequest,
                                                                    managedObjectContext: context,
                                                                    sectionNameKeyPath: "sectionDate",
                                                                    cacheName: nil)
            fetchResultsController.delegate = self
            
            return fetchResultsController
        } else {
            return nil
        }
    }
...
}

Conversation model:

class Conversation: NSManagedObject {
    @NSManaged public var createdAt: NSDate?
    @NSManaged public var uuid: String?
    @NSManaged public var messages: NSSet?
}

Messages model:

class Message: NSManagedObject {
    @NSManaged public var createdAt: NSDate?
    @NSManaged public var sectionDate: String?
    @NSManaged public var conversation: Conversation?
}

Crash stack trace:

Crashed: com.apple.main-thread
EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x000000082a3ab680
Crashed: com.apple.main-thread
0  libobjc.A.dylib                0x307c objc_retain_x8 + 16
1  CoreData                       0x4a7f8 __43-[NSFetchedResultsController performFetch:]_block_invoke + 660
2  CoreData                       0x837e8 developerSubmittedBlockToNSManagedObjectContextPerform + 156
3  CoreData                       0x83338 -[NSManagedObjectContext performBlockAndWait:] + 208
4  CoreData                       0x60688 -[NSFetchedResultsController _recursivePerformBlockAndWait:withContext:] + 152
5  CoreData                       0x5e574 -[NSFetchedResultsController performFetch:] + 252
6  MyPsychic                      0x3a224 MessagesViewController.performNewFetch() + 277 (MessagesViewController.swift:277)
7  MyPsychic                      0x39e38 MessagesViewController.viewDidLoad() + 4338048568 (<compiler-generated>:4338048568)
8  MyPsychic                      0x3ac24 @objc MessagesViewController.viewDidLoad() + 4338052132 (<compiler-generated>:4338052132)
9  UIKitCore                      0x382dc4 -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 84
10 UIKitCore                      0x1ae40 -[UIViewController loadViewIfRequired] + 712
11 UIKitCore                      0x1ab58 -[UIViewController view] + 24
12 UIKitCore                      0x2e0f04 -[UINavigationController _startCustomTransition:] + 892
13 UIKitCore                      0x1a6f44 -[UINavigationController _startDeferredTransitionIfNeeded:] + 496
14 UIKitCore                      0x1a6604 -[UINavigationController __viewWillLayoutSubviews] + 96
15 UIKitCore                      0x1a6568 -[UILayoutContainerView layoutSubviews] + 172
16 UIKitCore                      0x5020 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1980
17 QuartzCore                     0x99ec CA::Layer::layout_if_needed(CA::Transaction*) + 500
18 QuartzCore                     0x1d0a0 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 148
19 QuartzCore                     0x2e5b0 CA::Context::commit_transaction(CA::Transaction*, double, double*) + 456
20 QuartzCore                     0x635ec CA::Transaction::commit() + 652
21 QuartzCore                     0x4c8cc CA::Transaction::flush_as_runloop_observer(bool) + 88
22 UIKitCore                      0x504b44 _UIApplicationFlushCATransaction + 52
23 UIKitCore                      0x652740 _UIUpdateSequenceRun + 84
24 UIKitCore                      0xc99fd0 schedulerStepScheduledMainSection + 172
25 UIKitCore                      0xc9919c runloopSourceCallback + 92
26 CoreFoundation                 0xd5f54 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28
27 CoreFoundation                 0xe232c __CFRunLoopDoSource0 + 176
28 CoreFoundation                 0x66210 __CFRunLoopDoSources0 + 244
29 CoreFoundation                 0x7bba8 __CFRunLoopRun + 836
30 CoreFoundation                 0x80ed4 CFRunLoopRunSpecific + 612
31 GraphicsServices               0x1368 GSEventRunModal + 164
32 UIKitCore                      0x3a23d0 -[UIApplication _run] + 888
33 UIKitCore                      0x3a2034 UIApplicationMain + 340
34 MyPsychic                      0x9368 main + 16 (AppDelegate.swift:16)
35 ???                            0x1ecbe4960 (Missing)
Post not yet marked as solved Up vote post of SergeyNix Down vote post of SergeyNix
641 views