objective c - iOS - Core data : Fetch and delete , one row at a time -
i have objects stored in core data. want fetch objects 1 row @ time , after finish operation on it, want delete row/object core data. , again fetch next row , delete row, , on until core data empty. (with approach) code store objects in core data :
-(bool)saveproduct:(addproduct *)addproduct withimagensdata:(nsdata *)imagensdata error:(nserror *)error{ nsmanagedobject *object = [nsentitydescription insertnewobjectforentityforname:@"device" inmanagedobjectcontext:self.managedobjectcontext]; device *device = (device *)object; [device setvalue:addproduct.currencytype forkey:@"currencytype"]; [device setvalue:[nsnumber numberwithdouble:addproduct.latitude] forkey:@"latitude"]; [device setvalue:[nsnumber numberwithdouble:addproduct.longitude] forkey:@"longitude"]; [device setvalue:[nsnumber numberwithdouble:addproduct.price] forkey:@"price"]; return [self.managedobjectcontext save:&error]; }
this fetch of devices , iterate through them, deleting objects after you've performed work on them:
nsmanagedobjectcontext *context = self.managedobjectcontext; nsfetchrequest *fetchrequest = [nsfetchrequest fetchrequestwithentityname:@"device"]; //fetch of devices nserror *error; nsarray *alldevices = [context executefetchrequest:fetchrequest error:&error]; if (error) { nslog(@"error fetching devices! %@", error); return; } (device *device in alldevices) { // perform operation on device [context deleteobject:device]; } [context save:null];
Comments
Post a Comment