Core Data crashes in iOS 11 when using a key path in compound indexes

Originator:emilio.pavia
Number:rdar://33180744 Date Originated:July 7 2017, 4:20 PM
Status:Resolved Resolved:August 16 2017, 9:44 AM
Product:iOS + SDK Product Version:11.0 (15A5304i)
Classification:Crash Reproducible:Always
 
We distribute a dynamic framework (in binary form) for iOS. It uses Core Data and includes a compiled core data model. Up to iOS 10.x we hadn't any issue, but on iOS 11 the NSPersistentStoreCoordinator crashes when trying to add the persistent store.

After a deep debug I found the root cause of the crash: we have several compound indexes in our entities, almost all plain properties. A couple of them are instead a key path on a relationship. For example a Message entity has as compound index the value "date, sender.email".

With this setup the app crashes with the following log:

CoreData: sql: CREATE INDEX IF NOT EXISTS ZMESSAGE_ZSENDER_INDEX ON ZMESSAGE (ZSENDER)
CoreData: sql: CREATE INDEX IF NOT EXISTS Z_Message_date_sender.email ON ZMESSAGE (ZDATE COLLATE BINARY ASC)
2017-07-07 16:02:52.470597+0200 CoreDataRadar[695:70636] [logging] unknown database Z_Message_date_sender
CoreData: annotation: Disconnecting from sqlite database due to an error.
2017-07-07 16:02:52.476023+0200 CoreDataRadar[695:70636] [error] error: (1) I/O error for database at /var/mobile/Containers/Data/Application/753BEC83-EE52-460B-817E-62225A2F4928/Library/Application Support/CoreDataRadar.sqlite.  SQLite error code:1, 'unknown database Z_Message_date_sender'
CoreData: error: (1) I/O error for database at /var/mobile/Containers/Data/Application/753BEC83-EE52-460B-817E-62225A2F4928/Library/Application Support/CoreDataRadar.sqlite.  SQLite error code:1, 'unknown database Z_Message_date_sender'

It seems that it can't handle the key path separator in the SQL statement.

It's important to note that if the model is built and run using Xcode 9, the generated SQL statements are the following:

CoreData: sql: CREATE INDEX IF NOT EXISTS ZMESSAGE_ZSENDER_INDEX ON ZMESSAGE (ZSENDER)
CoreData: sql: CREATE INDEX IF NOT EXISTS ZUSER_ZMESSAGES_INDEX ON ZUSER (ZMESSAGES)
CoreData: sql: CREATE INDEX IF NOT EXISTS Z_User_email ON ZUSER (ZEMAIL COLLATE BINARY ASC)

When building and running on iOS 10 with Xcode 8 the generated SQL statements are the following:

CoreData: sql: CREATE INDEX IF NOT EXISTS ZMESSAGE_ZDATE_INDEX ON ZMESSAGE (ZDATE)
CoreData: sql: CREATE INDEX IF NOT EXISTS ZMESSAGE_ZSENDER_INDEX ON ZMESSAGE (ZSENDER)
CoreData: sql: CREATE INDEX IF NOT EXISTS ZMESSAGE_ZDATE ON ZMESSAGE (ZDATE)
CoreData: sql: CREATE INDEX IF NOT EXISTS ZUSER_ZEMAIL_INDEX ON ZUSER (ZEMAIL)

Please, although we plan to release an update of our framework built with Xcode 9, consider fixing this bug in iOS 11 when using a model built with Xcode 8, as we have a very large user base that will use our old binary when iOS 11 will be released next fall.

Thank you!

Steps to Reproduce:
- Open the attached project with Xcode 8
- Launch on a device with iOS 11 (https://stackoverflow.com/questions/44381860/use-xcode-8-with-ios-11)

Expected Results:
The database should be created without errors.

Observed Results:
The app crashes.

Comments

Apple Developer Relations

We believe this issue has been resolved in the latest macOS 10.13 beta.

Please test with the latest beta. If you still have issues, please update your bug report with any relevant logs or information that could help us investigate.

By emilio.pavia at Aug. 16, 2017, 7:45 a.m. (reply...)

Apple Developer Relations

A solution is under investigation. We will follow up with you again when it is available.

By emilio.pavia at Aug. 16, 2017, 7:45 a.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!