title: How to get a NSBatchDeleteRequest to delete and allow you to add back to an object again
body: <p>I have the following code.</p>
  
  <pre><code>let workorderFetchRequest   = NSFetchRequest&lt;NSFetchRequestResult&gt;(entityName: "Workorders")
                  let deleteWorkOrderRequest  = NSBatchDeleteRequest(fetchRequest: workorderFetchRequest) //Deletes ALL workorders
  
                  //Question is will it delete all references to the work orders correctly now?
                  try context.execute(deleteWorkOrderRequest)
  
                  //Save the work order objects (overwriting any old ones with same id if needed)
                  do {
                      try context.save()
                      print("Saved context with workorders")
                  } catch let error as NSError  {
                      print("Could not save \(error), \(error.userInfo)")
                  } catch {
                      print("Could not save, unknown error")
                  }
  
                  print("deleted references")
  </code></pre>
  
  <p>A workorders looks like this...</p>
  
  <pre><code>import Foundation
  import CoreData
  
  extension Workorders {
  
      @nonobjc public class func fetchRequest() -&gt; NSFetchRequest&lt;Workorders&gt; {
          return NSFetchRequest&lt;Workorders&gt;(entityName: "Workorders");
      }
  
      @NSManaged public var address: String?
      @NSManaged public var client_id: Int64
      @NSManaged public var desc: String?
      @NSManaged public var id: Int64
      @NSManaged public var phone: String?
      @NSManaged public var signaturepath: String?
      @NSManaged public var lat: String?
      @NSManaged public var lng: String?
      @NSManaged public var service: Service?
      @NSManaged public var pictures: NSSet?
      @NSManaged public var videos: NSSet?
  
  }
  
  // MARK: Generated accessors for pictures
  extension Workorders {
  
      @objc(addPicturesObject:)
      @NSManaged public func addToPictures(_ value: Pictures)
  
      @objc(removePicturesObject:)
      @NSManaged public func removeFromPictures(_ value: Pictures)
  
      @objc(addPictures:)
      @NSManaged public func addToPictures(_ values: NSSet)
  
      @objc(removePictures:)
      @NSManaged public func removeFromPictures(_ values: NSSet)
  
  }
  
  // MARK: Generated accessors for videos
  extension Workorders {
  
      @objc(addVideosObject:)
      @NSManaged public func addToVideos(_ value: Videos)
  
      @objc(removeVideosObject:)
      @NSManaged public func removeFromVideos(_ value: Videos)
  
      @objc(addVideos:)
      @NSManaged public func addToVideos(_ values: NSSet)
  
      @objc(removeVideos:)
      @NSManaged public func removeFromVideos(_ values: NSSet)
  
  }
  </code></pre>
  
  <p>I have it referencing a service object that looks like this...</p>
  
  <pre><code>import Foundation
  import CoreData
  
  extension Service {
  
      @nonobjc public class func fetchRequest() -&gt; NSFetchRequest&lt;Service&gt; {
          return NSFetchRequest&lt;Service&gt;(entityName: "Service");
      }
  
      @NSManaged public var id: Int64
      @NSManaged public var name: String?
      @NSManaged public var templatedata: String?
      @NSManaged public var workorder: Workorders?
  
  }
  </code></pre>
  
  <p>When I run the delete code I have on the model for workorder it set to <strong>Nullify as the delete rule</strong> ... (refer to image below)</p>
  
  <p><a href="https://i.stack.imgur.com/KNeVH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KNeVH.png" alt="enter image description here"></a></p>
  
  <p>When I run the batch delete however it does not seem to be nullifying service correctly.  When I attempt to set a new service onto the workorder it crashes with...</p>
  
  <p><strong>libc++abi.dylib: terminating with uncaught exception of type NSException</strong></p>
  
  <p>I'm sure this is the case because <strong>if I set the delete rule to cascade on the workorders then it fixes the issue</strong>.  However, I want to keep the old services just nullify the reference.</p>
  
  <p>Why can I not set a new service onto the workorder without it crashing?</p>
  
  <p>Attached below is the area in code it crashes by looking at the console messages I've narrowed it down to the moment I attempt to set service.</p>
  
  <p><a href="https://i.stack.imgur.com/CkZ6f.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CkZ6f.png" alt="enter image description here"></a></p>
  
  <p>Also note, I'm using a <strong>NSMergeByPropertyObjectTrumpMergePolicy</strong> since I intend to set the same ID service to the workorder again, so when I create a new service with the same ID it should just overwrite the data of the old service if my understanding is correct.  Could this be the issue, and if so how do I fix it?</p>