ios - iOS9 Xcode 7 - Core Data - avoiding duplicated objects -
as described in wwdc2015 presentation video, in new xcode7 can set objects uniqueness directly in xcode model editor. trying implement code, not working expected. when try save duplicated object, xcode rejects save, table updates duplicated cell.
so have set unique attributes startdate , enddate.
then have modified save function handle error , inform user uialertcontroller.
func addcontract() { { let appdelegate: appdelegate = uiapplication.sharedapplication().delegate as! appdelegate let context: nsmanagedobjectcontext = appdelegate.managedobjectcontext let entity = nsentitydescription.entityforname("contract", inmanagedobjectcontext: context) let newcontractdata = contract(entity: entity!, insertintomanagedobjectcontext: context) newcontractdata.startdate = dateformatter.datefromstring(startdatetextfield.text!)! newcontractdata.enddate = dateformatter.datefromstring(enddatetextfield.text!)! newcontractdata.ship = shipnametextfield.text! newcontractdata.position = positiononboardtextfield.text! newcontractdata.workingdays = int(workingdayslabel.text!)! try context.save() } catch { let alertcontroller = uialertcontroller( title: "error", message: "the contract exsist", preferredstyle: uialertcontrollerstyle.alert) let okaction = uialertaction( title: "ok", style: uialertactionstyle.cancel, handler: nil) alertcontroller.addaction(okaction) presentviewcontroller(alertcontroller, animated: true, completion: nil) } }
so far good, when go root controller cancel button, table appears updated duplicate cell.
@ibaction func cancelbuttonpressed(sender: uibarbuttonitem) { self.navigationcontroller?.poptorootviewcontrolleranimated(true) }
in addition, stop , run application removes duplicates.
here video of problematic behaviour.
the generated error follow:
error domain=nscocoaerrordomain code=1551 "the operation couldn’t completed. (cocoa error 1551.)" userinfo=0x7fc02d462190 {conflicts=( { constraint = ( startdate, enddate ); entity = contract; objects = ( "<contract: 0x7fc02d45ba60> (entity: contract; id: 0x7fc02d019430 <x-coredata:///contract/t0897571b-200b-4f04-af87-d50831e2de672> ; data: {\n enddate = \"2017-06-13 21:00:00 +0000\";\n position = test;\n ship = test;\n startdate = \"2016-06-13 21:00:00 +0000\";\n workingdays = 366;\n})", "<contract: 0x7fc02b7433c0> (entity: contract; id: 0xd000000000100000 <x-coredata://c3318932-bedb-4ab6-a856-103f542bcf44/contract/p4> ; data: {\n enddate = \"2017-06-13 21:00:00 +0000\";\n position = test;\n ship = test;\n startdate = \"2016-06-13 21:00:00 +0000\";\n workingdays = 366;\n})" ); } )} 2015-06-14 19:54:15.880 workingdays[6028:2219449] poptoviewcontroller:transition: called on <uinavigationcontroller 0x7fc02c007e00> while existing transition or presentation occurring; navigation stack not updated.
the modification of addcontract() fix problem follow:
func addcontract() { let appdelegate: appdelegate = uiapplication.sharedapplication().delegate as! appdelegate let context: nsmanagedobjectcontext = appdelegate.managedobjectcontext let entity = nsentitydescription.entityforname("contract", inmanagedobjectcontext: context) let newcontractdata = contract(entity: entity!, insertintomanagedobjectcontext: context) { newcontractdata.startdate = dateformatter.datefromstring(startdatetextfield.text!)! newcontractdata.enddate = dateformatter.datefromstring(enddatetextfield.text!)! newcontractdata.ship = shipnametextfield.text! newcontractdata.position = positiononboardtextfield.text! newcontractdata.workingdays = int(workingdayslabel.text!)! try context.save() } catch { let alertcontroller = uialertcontroller( title: "error", message: "the contract exsist", preferredstyle: uialertcontrollerstyle.alert) let okaction = uialertaction( title: "ok", style: uialertactionstyle.cancel, handler: nil) alertcontroller.addaction(okaction) presentviewcontroller(alertcontroller, animated: true, completion: nil) context.deleteobject(newcontractdata) print(error) } }
are using nsfetchedresultscontroller
show data?
it seems uniqueness ensured when save. core data still allows insert object nsmanagedobjectcontext
when do:
let newcontractdata = contract(entity: entity!, insertintomanagedobjectcontext: context)
when save, save operations fails, object still in context, nsfetchedresultscontroller
still displays it.
try remove object context in catch code:
context.deleteobject(newcontractdata)
Comments
Post a Comment