18

My app uses core data. I have recently upgraded to Xcode 10.2 and swift 5 and since then I am receiving random crashes that has something to do with core data.

From what I have gathered this happened when trying to change Core Data from a background thread (After pulling new data from the server).

I receive the following error message

2019-03-31 14:49:17.358685+0300 LeaderMES[24226:595701] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSTaggedDate objectForKey:]: unrecognized selector sent to instance 0x8000000000000000'

Or

2019-03-31 14:37:04.676485+0300 LeaderMES[23749:583097] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSCoreDataTaggedObjectID objectForKey:]: unrecognized selector sent to instance 0x8000000000000000'

Not only my code used to work, this instance number looks suspicious

My app is connected to crashlytics which caught one of these errors. Here is the stack trace it caught:

Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x1086f86e3 (Missing)
1  libobjc.A.dylib                0x10771bac5 objc_exception_throw
2  CoreFoundation                 0x108716ab4 (Missing)
3  CoreFoundation                 0x1086fd443 (Missing)
4  CoreFoundation                 0x1086ff238 (Missing)
5  libswiftCore.dylib             0x109914dcc (Missing)
6  libswiftCore.dylib             0x109b407b9 (Missing)
7  LeaderMES                      0x105080a8d closure #1 in LMNotificationRepository.loadNotificationHistory(forFactory:successCompletion:errorCompletion:) (LMNotificationRepository.swift:360)
8  LeaderMES                      0x105091271 partial apply for closure #1 in LMNotificationRepository.loadNotificationHistory(forFactory:successCompletion:errorCompletion:) (<compiler-generated>)
9  LeaderMES                      0x10510b872 closure #1 in LMHttpProvider.procedeRequest(_:completionHandler:) (LMHTTPProvider.swift:299)
10 LeaderMES                      0x10510e381 partial apply for closure #1 in LMHttpProvider.procedeRequest(_:completionHandler:) (<compiler-generated>)
11 LeaderMES                      0x1050ce176 thunk for @escaping @callee_guaranteed (@guaranteed Data?, @guaranteed NSURLResponse?, @guaranteed Error?) -> () (<compiler-generated>)
12 CFNetwork                      0x10adf6178 (Missing)
13 CFNetwork                      0x10ae0cc56 (Missing)
14 Foundation                     0x10666f412 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__
15 Foundation                     0x10666f31a -[NSBlockOperation main]
16 Foundation                     0x10666c1f4 -[__NSOperationInternal _start:]
17 Foundation                     0x106671f5b __NSOQSchedule_f
18 libdispatch.dylib              0x10a539ccf (Missing)
19 libdispatch.dylib              0x10a53ad02 (Missing)
20 libdispatch.dylib              0x10a53d6be (Missing)
21 libdispatch.dylib              0x10a53cd49 (Missing)
22 libdispatch.dylib              0x10a549ad3 (Missing)
23 libdispatch.dylib              0x10a54a330 (Missing)
24 libsystem_pthread.dylib        0x10a91c6b3 (Missing)
25 libsystem_pthread.dylib        0x10a91c3fd (Missing)

What are all the missing dylibs mentioned?

I have tried moving all Core Data activity to main thread using DispatchQueue with no luck.

I have removed the app from simulator and reinstalled it and so far the crash doesn't repeat. Any ideas as to what caused this crash?

Yoni Reiss
  • 320
  • 2
  • 8
  • 1
    "-com.apple.CoreData.ConcurrencyDebug 1": set this argument in Xcode’s scheme editor to enable Core Data Debugging w.r.t thread. This can also help to identify crash reasons. – Anand Apr 01 '19 at 16:03
  • Have you managed to resolve it ? I have almost the exact same issue after upgrading Xcode to 10.2 or higher. THanks! – mentoxska Aug 22 '19 at 08:05

3 Answers3

9

A similar issue was happening for me when I was changing and accessing an object from two different threads (in two different places in my code) at the same time, address must have been changing. I solved by using a DispatchQueue to sync the access to this object

var lock: DispatchQueue = DispatchQueue.init(label: "")

lock.sync{ access object }

Adam Smith
  • 189
  • 1
  • 3
8

It looks like a bug for non-optimized builds done in Xcode 10.2. I don't use a Core Data in my app and it also crashes with

-[xxx objectForKey:]: unrecognized selector sent to instance 0x8000000000000000'

XXX sometimes is __NSTaggedDate, sometimes it is another type but the address is always 0x8000000000000000. Debugger stops on a line when I access a valid dictionary by a valid key and it is not helpful at all. The app stops crashing when I changed optimization to Optimise for speed -O for debug scheme.

darekn
  • 81
  • 2
  • Thanks for the idea, I'll try. In my case too it comes in different types, always with the same address, although it usually appears around the same code. Problem is it happens in release mode too, so I'll have to check what happens there. – Yoni Reiss Apr 02 '19 at 09:44
  • 2
    I had this exact issue, however `Optimise for speed -O` didn't seem to resolve the issue. For me, the issue was happening due to the dictionary access being executed on a background thread. – jnelson Apr 02 '19 at 16:40
  • What's the problem with accessing a dictionary from a background thread? By the way I implemented a similar solution - accessing a dictionary only from a designated queue to avoid conflicts. I still hope it solves the issue. – Yoni Reiss Apr 02 '19 at 17:04
  • In my case the issue occurs after upgrading to Xcode 10.2 and I am still using Swift 4.2. The same codebase worked without any problem in the previous release of Xcode and I am accessing the dictionary in the main thread. – darekn Apr 02 '19 at 21:58
0

You are sending a method objectForKey: which is typically used for dictionaries. The receiver however is a TaggedDate object. TaggedDate is basically the same as NSDate (for our purposes here). So somehow you managed to have an NSDate object where you expect a dictionary.

Add a breakpoint on exception, so you can step back to the caller, and figure out why you have an NSDate object where you expected a dictionary. Expect a line like dict [@"some key"] when dict turns out to be an NSDate.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • 2
    The question is where. None of the entities in the model have a date field, nor do I use date on one of those functions. This also does not explain why the sudden crash after the update, nor why it has stopped after reinstalling of the app without any changes to the code. – Yoni Reiss Mar 31 '19 at 19:29
  • @YoniReiss I'm giving you advice how to solve the problem, you reply why the problem cannot possibly arise when in fact it does. If it crashes for reason X then saying "it cannot crash for reason X" won't get you anywhere. – gnasher729 Mar 04 '20 at 17:47