我有一些对象存储在核心数据中。现在我想一次获取所有对象,但是在我完成操作之后,我想从核心数据中仅删除该行/对象。然后再次获取下一行并删除该行,依此类推,直到核心数据为空。 (使用好的方法)我的代码将对象存储在核心数据中:
-(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];
}
答案 0 :(得分:0)
这将获取所有设备并迭代它们,在您完成工作后删除对象:
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];