ios - How to get a NSBatchDeleteRequest to delete and allow you to add back to an object again -


i have following code.

let workorderfetchrequest   = nsfetchrequest<nsfetchrequestresult>(entityname: "workorders")                 let deleteworkorderrequest  = nsbatchdeleterequest(fetchrequest: workorderfetchrequest) //deletes workorders                  //question delete references work orders correctly now?                 try context.execute(deleteworkorderrequest)                  //save work order objects (overwriting old ones same id if needed)                 {                     try context.save()                     print("saved context workorders")                 } catch let error nserror  {                     print("could not save \(error), \(error.userinfo)")                 } catch {                     print("could not save, unknown error")                 }                  print("deleted references") 

a workorders looks this...

import foundation import coredata  extension workorders {      @nonobjc public class func fetchrequest() -> nsfetchrequest<workorders> {         return nsfetchrequest<workorders>(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 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 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)  } 

i have referencing service object looks this...

import foundation import coredata  extension service {      @nonobjc public class func fetchrequest() -> nsfetchrequest<service> {         return nsfetchrequest<service>(entityname: "service");     }      @nsmanaged public var id: int64     @nsmanaged public var name: string?     @nsmanaged public var templatedata: string?     @nsmanaged public var workorder: workorders?  } 

when run delete code have on model workorder set nullify delete rule ... (refer image below)

enter image description here

when run batch delete not seem nullifying service correctly. when attempt set new service onto workorder crashes with...

libc++abi.dylib: terminating uncaught exception of type nsexception

i'm sure case because if set delete rule cascade on workorders fixes issue. however, want keep old services nullify reference.

why can not set new service onto workorder without crashing?

attached below area in code crashes looking @ console messages i've narrowed down moment attempt set service.

enter image description here

also note, i'm using nsmergebypropertyobjecttrumpmergepolicy since intend set same id service workorder again, when create new service same id should overwrite data of old service if understanding correct. issue, , if how fix it?

i find issues trying.

at first, think need revise entity names. should single type.

for example, used "workorders" or "pictures" them. should "workorder" , "picture".

and main issue synced managedobjects before setup right relationship.

for example, created 1 service object , workorder relationship nil yet. in point, saved context. happen if had optional relationship.

and created workorder object , bind existing service relationship.

core data tries bind new workorder workorder relationship of service object vise versa.

so should done in 1 transaction. means need save context once after complete setting relationship.

so remove savecontext line service.

lets did did now. after save context new service, created workorder1 setting service relationship. , app crashed or has been stopped something. causes data integrity issue in cases.

i hope understanding issue , solved it.

cheers!


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -