Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Under the Hood - Core Data

Under the Hood - Core Data

Swift Bengaluru

jatinarora

April 14, 2018
Tweet

Other Decks in Education

Transcript

  1. WHAT WILL WE COVER TODAY? ▸ How does a Fetch

    Request work? ▸ How does Fetch Batch Size work? ▸ How does a Fetch Batch Request work? ▸ What happens when you ‘Fault’ an object? ▸ Extra Special - Relationship faults ▸ Performance characteristics of Core Data ▸ Tips for High Performance with Core Data
  2. Context Coordinator Persistent Store SQLite execute(_ request:) with batch size

    executes query to fetch all objectIDs Loads all objectIDs into memory Special array backed by objectIDs fetchedObjects Array
  3. Context Coordinator Persistent Store SQLite Executes query to fetch the

    objects Loads all objects into memory Object at index & surrounding objects retrieved objectAtIndexPath(_ indexPath:) Loads fetchBatchSize objects around the accessed index
  4. CAVEATS OF USING BATCH REQUESTS ▸ The data in contexts

    and Persistent Store’s row-cache are out of date. ▸ Just refreshing won’t help because the objects would be turned into faults but the data in row-cache will still be older. ▸ Solution: ▸ Use mergeChanges(fromRemoteContextSave:into:) on the MOC with the [NSUpdatedObjectsKey: objectIDs]. ▸ Under the hood, this method updates the row-cache as well. :)
  5. RELATIONSHIP FAULTS ▸ To-one relationship act like normal faults. The

    relationships have objectIDs through which faults are fulfilled. ▸ To-many relationships work on two-level faulting. First when you access the relationship, the first level fault is fired and objectIDs for the relationship are loaded into the memory. ▸ Second-level faulting occurs like normal fault when any relationship object’s property is being accessed.
  6. TIPS TO IMPROVE PERFORMANCE ▸ Use materialised objects method. ▸

    Use indexing and compound indexing. ▸ It’s okay to do de-normalization in order to make your UI faster. ▸ Put simpler and more performant predicates first as it reduces the sample set. ▸ Use batch requests. It’s a little difficult to get right but once you get a hang of it. It just works. :)