Fixing the Mysteriously Missing Core Data Objects

Fixing the Mysteriously Missing Core Data Objects

The below post helps you to identify and fix the mysteriously missing Core Data objects. I have also provided a link with a working project where I demonstrate how objects go missing and how it can be fixed.

Checklist for the solution below

  • You are getting <x-coredata: UDID; data: fault>, despite setting fetchRequest.returnsObjectsAsFaults = false and saving the managed object context properly
  • Your objects and relationships are available on first run and go missing once you restart/relaunch the app — or sometimes a few objects and relationships are randomly available and start missing in subsequent runs
  • You don’t have any other errors while saving the managed object context
  • The objects are missing where you don’t have an inverse relationship, and Xcode is also indicating a warning about the missing inverse relationship

When Does It Happen

Most cases follow a similar pattern. Check if it suits your situation:

  • A has a one-to-many relationship with B. B doesn’t have any inverse relationship.
  • B’s objects are inserted first and saved to the store.
  • A’s objects are created in context, B’s objects are fetched and mapped to A.
  • When you save the context, it appears A is saved along with its relationship to B’s objects.
  • But when you fetch immediately from the store, A’s relationship to B is missing and you cannot access B’s objects from A.

Solution

There are 2 solutions:

  1. B should have an inverse relationship to A. (Recommended if possible.)

    (or)

  2. Ensure that A’s object is first saved to the store as soon as it’s created.

    • Now fetch A’s object, map it to existing B objects, and save it to the store.
    • When you fetch/refresh A’s object, you will be able to access B’s objects despite not having an inverse relationship.

Projects in This Repo

There are 2 projects in my repo to demonstrate the above solution:

  1. OneToMany-NotWorking — demonstrates why it’s not working
  2. OneToMany-Working — demonstrates how to make it work

Citation

Please provide citation to this repo in Stack Overflow or wherever required.

Related Posts

Fixing 'exportArchive: No iOS In-House / Ad Hoc Profiles for Team'

Fixing 'exportArchive: No iOS In-House / Ad Hoc Profiles for Team'

Below is a common error when trying to set up CI/CD using Fastlane or raw XcodeBuild/Xcrun commands:"error: exportArchive: No "iOS In House" profiles for team" or "error: exportArchive: No "adhoc

read more
Part 1 — Using Edge ML in iOS/Android: Building a Smart Savings App with Transaction Text Classification

Part 1 — Using Edge ML in iOS/Android: Building a Smart Savings App with Transaction Text Classification

Introduction This tutorial demonstrates how to build a text classification system for bank transactions using TensorFlow and deploy it on mobile platforms. The system automatically categorizes tra

read more
Part 2 — Using Edge ML in iOS: Building a Smart Savings App with Transaction Text Classification

Part 2 — Using Edge ML in iOS: Building a Smart Savings App with Transaction Text Classification

iOS Implementation with TensorFlow Lite This section demonstrates integrating the trained TensorFlow Lite model into an iOS application using Swift. Project Setup Add TensorFlow Lite depende

read more