Swift core data code that only works sometimes -
i wrote code set value 1 on property of xcdatamodeld entity called variables in viewdidload. problem when code runs, half time res.valueforkey("runningforfirsttime"), equals nil , half time res.valueforkey("runningforfirsttime") equals 1. why happening? here code:
var appdel:appdelegate = uiapplication.sharedapplication().delegate as! appdelegate var context:nsmanagedobjectcontext = appdel.managedobjectcontext! var request = nsfetchrequest(entityname: "variables") request.returnsobjectsasfaults = false var newvar: anyobject = nsentitydescription.insertnewobjectforentityforname("variables", inmanagedobjectcontext: context) var results:array = context.executefetchrequest(request, error: nil)! var res = results[0] as! nsmanagedobject println(res.valueforkey("runningforfirsttime")) if res.valueforkey("runningforfirsttime") == nil{ //some code println("this nil") } newvar.setvalue(1, forkey: "runningforfirsttime") println(res.valueforkey("runningforfirsttime"))
var request = nsfetchrequest(entityname: "variables") request.returnsobjectsasfaults = false ok have fetch request find variables entities.
var newvar: anyobject = nsentitydescription.insertnewobjectforentityforname("variables", inmanagedobjectcontext: context) then insert new instance of variables entity. there 1 or more variables in our context.
var results:array = context.executefetchrequest(request, error: nil)! now execute fetch request retrieve of variables instances...
var res = results[0] as! nsmanagedobject ...and grab whichever variables happens first result.
every time run code block insert yet variables entity context. might fetch instance had used has had runningforfirsttime property set or might object created has not.
Comments
Post a Comment