project-imas / encrypted-core-data

v2.0 - iOS Core Data encrypted SQLite store using SQLCipher
Other
785 stars 236 forks source link

Bug in iOS14 batch fetches #335

Open egrim opened 3 years ago

egrim commented 3 years ago

We found a crash condition on iOS14 when compiled with Xcode 12.1's toolchain (as of November 17, 2020). Trigger conditions are reproducible with this sequence of events:

  1. Create a child context with a parent context tied to the persistent store coordinator
  2. Create objects within the child context and save them to the parent (but not to the persistent store).
  3. Perform a fetch request with a batch limit within the child context
  4. Access one of the results

Starting in iOS14, Core Data will fire a fault request upon accessing one of these results. This request contains a predicate with unsubstituted variables. While the request object contains the variables needed to perform the substitution, I haven't yet found a clean way to access them.

In the course of fixing this bug, I encountered an additional issue that occurs when a predicate includes a collection of unpersisted objects. These objects do not have permanent IDs yet, but EncryptedCoreData code attempts to bind them to the SQL statement, resulting in an error. Existing code handled cases where a predicate includes a single instance of an unpresisted object, but did not handle collections of them. To fix this, I moved the code that successfully handles single objects in parsing expression into the binding method instead.