Reputation: 848
I have some objects stored in core data. Now I want to fetch all objects but one row at a time and after i finish my operation on it, I want to delete only that row/object from core data. And again fetch next row and delete that row, and so on until core data is empty. (with good approach) My code to 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];
}
Upvotes: 1
Views: 304
Reputation: 71
This will fetch all of the Devices and iterate through them, deleting the objects after you've performed your work on them:
NSManagedObjectContext *context = self.managedObjectContext;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Device"];
//Fetch all of the Devices
NSError *error;
NSArray *allDevices = [context executeFetchRequest:fetchRequest error:&error];
if (error) {
NSLog(@"Error fetching devices! %@", error);
return;
}
for (Device *device in allDevices) {
// Perform your operation on the device
[context deleteObject:device];
}
[context save:NULL];
Upvotes: 0