title: How to Save to a Custom Join Table Core Data (many-to-many) without unique predicate - ManagedObjectID (Swift)?
body: <p>I will be super thankful for any help. How can I save instances to a join table without a unique identifier as a predicate? Can I use the managed object id to check if the item exists already?</p>
  
  <p>I'm building an app with different exercise plans. Each plan holds many exercise, and an exercise can belong to many plans. I have structured my data model to include a custom join table so that I can query the completion status of an exercise from within one plan.
  <a href="https://i.stack.imgur.com/IiN4a.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IiN4a.png" alt="enter image description here"></a></p>
  
  <p>I'm sourcing my data from a json file and would like to save it to core data. I'm able to correctly save my CoreExercise, and CorePlan tables, however am having difficulty understanding how to save the instance of the object in the intermediate join table, since I'm unsure of what predicate to use.
  I've written a class function to check if the instance exists, and to save it if it doesn't.</p>
  
  <pre><code>class CoreExercisePlan: NSManagedObject {
    class func coreExercisesForExercisePlan(exerciseInfo: Exercise, planName: String, inManagedObjectContext context: NSManagedObjectContext) -&gt; CoreExercisePlan? {
  
      let request = NSFetchRequest(entityName: "CoreExercisePlan")
      request.predicate = NSPredicate() // Search for ObjectID here? / How?
  
      if let exercisePlan = (try? context.executeFetchRequest(request))?.first as? CoreExercisePlan {
          print("we have this exercise plan already saved")
          return exercisePlan
  
      } else if let exercisePlan = NSEntityDescription.insertNewObjectForEntityForName("CoreExercisePlan", inManagedObjectContext: context) as? CoreExercisePlan {
          exercisePlan.status = 0
          exercisePlan.progress = 0
          print("we are creating new object")
          return exercisePlan
      }
      return nil
  }
  
  
  private func updateDatabaseWithExercisePlans(){
      managedObjectContext?.performBlock {
  
          // Array of exercises for each plan:
          let coffeePlanExercises = self.coffeeExercises 
          let subwayPlanExercises = self.subwayExercises
  
          for exercise in coffeePlanExercises {
              _ = CoreExercisePlan.coreExercisesForExercisePlan(exercise, planName: "coffee", inManagedObjectContext: self.managedObjectContext!)
          }
  
          for exercise in subwayPlanExercises {
              _ = CoreExercisePlan.coreExercisesForExercisePlan(exercise, planName: "subway", inManagedObjectContext: self.managedObjectContext!)
          }
  
          do {
              try self.managedObjectContext?.save()
          } catch let error {
              print("printing error here: \(error)")
          }
      }
  }
  </code></pre>
  
  <p>Is there a way to get the objectID of the instance in the join table, and use that as a predicate? Thanks!</p>