title: CoreData creates and saves extra entities with nil attributes
body: <p>I have a Contact table and Location table with many locations for each contact </p>
  
  <p>I create a Contact save and then create a Location and save and then assign the saved Location to the Contact. Code below:</p>
  
  <pre><code> @IBAction func saveLocation(_ sender: AnyObject) {
      let processName = ProcessInfo.processInfo.globallyUniqueString
  
      let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
      let location = Location(context: context)
  
      do {
  
          let fetchRequest: NSFetchRequest&lt;Contact&gt; = Contact.fetchRequest()
          fetchRequest.predicate = NSPredicate(format: "uniqueId == %@", contactIdentifierString)
          let fetchedResults = try context.fetch(fetchRequest)
          if let aContact = fetchedResults.first {
  
              // set location data
              location.uniqueId = processName
              location.locationName = locationNameTextField.text
              location.city = self.city
              location.state = self.state
              location.street = self.street
              location.zip = self.zip
              location.country = self.country
  
  
              // save data
              (UIApplication.shared.delegate as! AppDelegate).saveContext()
              location.contact = aContact
              myDelegate?.userSelectedContact(contactIdentifier: contactIdentifierString, locationIdentifier: processName)
  
          }
      }
      catch {
          print ("fetch task failed", error)
      }
  
  
  
  
      // Check results
      do {
  
          let fetchRequest: NSFetchRequest&lt;Location&gt; = Location.fetchRequest()
          fetchRequest.predicate = NSPredicate(format: "uniqueId == %@", processName)
          let fetchedResults = try context.fetch(fetchRequest)
          if let aLocation = fetchedResults.first {
              print("+++++==========++++++++++")
              print("Location.Contact:  \(aLocation.contact)")
              print("+++++==========++++++++++")
          }
      }
      catch {
          print ("location fetch failed")
      }
  
      self.navigationController!.popViewController(animated: true)
  }
  </code></pre>
  
  <p>I added just one Location, however when I print the contact entity I see two locations assigned as you can see below.</p>
  
  <pre><code>  Location.Contact:  Optional(&lt;rg2nrg.Contact: 0x6000002c73f0&gt; (entity: Contact; id: 0xd000000000240000 &lt;x-coredata://7D46FA65-2590-409D-89E7-995F64F07483/Contact/p9&gt; ; data: {
  email = vmail;
  firstName = vr;
  imageName = &lt;89504e47 0d0a1a0a 0000000d 49484452 0000012c 0000012c 08060000 00797d8e 75000000 01735247 4200aece 1ce90000 0009&gt;;
  lastName = r;
  locations =     (
      "0xd000000001780002 &lt;x-coredata://7D46FA65-2590-409D-89E7-995F64F07483/Location/p94&gt;",
      "0xd000000001740002 &lt;x-coredata://7D46FA65-2590-409D-89E7-995F64F07483/Location/p93&gt;"
  );
  phone = 22;
  providerName = tara;
  screenName = vr;
  uniqueId = "7B17C228-50A6-4309-BD0D-CBCD8E8FAEA1-1106-00000128477D4DB1";
  userType = Household;
    }))
  </code></pre>
  
  <p>Further, one of location entities has the correct the values of assigned attributes where as the other location entity has all nil values.</p>
  
  <p>This messes up my program. As I go over location entity attributes in a loop to delete them I now see three location entities</p>
  
  <pre><code> func deleteLocations() {
      var fetchedResults: [Location] = []
  
      let contextLocation = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
      do {
  
          fetchedResults = try contextLocation.fetch(Location.fetchRequest())
          print("number of locations to be deleted:: \(fetchedResults.count) ")
  
          if fetchedResults.count &gt; 0 {
  
              for location in fetchedResults{
                  print(location.locationName)
                  contextLocation.delete(location)
                  try contextLocation.save()
  
                  print(location.locationName)
              }
          }
      } catch {
          print ("fetch contact failed")
      }
  
  }
  </code></pre>
  
  <p> Output:
  number of locations to be deleted:: 3 </p>
  
  <p>In the later part of my code when I try to retrieve values it crashes because all the attributes of the two extra Location entities is all nil. </p>
  
  <p>My question:
  Because I added only one Location entity, there should only be one Location entity and that should be the only one linked to Contact. It shows two Locations assigned to Contact and then when I get count I have three Locations. Looks like Location entities with nil attributes keep getting added on its own.</p>
  
  <p>I am confused. Can someone help?</p>