0

我有这样的对象

@interface Clubs : NSManagedObject

@property (nonatomic, retain) NSString * catname;
@property (nonatomic, retain) NSString * clubdescription;
@property (nonatomic, retain) NSString * clubid;
@property (nonatomic, retain) NSString * clubname;
@property (nonatomic, retain) NSString * distance;
@property (nonatomic, retain) NSString * logopath;
@property (nonatomic, retain) NSString * phone;
@property (nonatomic, retain) NSString * clubtype;

@end

我只想删除那些具有特定cluid的对象?

假设有 20 个俱乐部被添加到 CoreData,只有我要删除的那些俱乐部 ID 为 120 的对象。

谢谢

4

2 回答 2

1
    -(void)deleteClubsEntity:(NSString*)clubId // pass the value 120
    {
        NSError *error = nil;
        NSManagedObjectContext *context = [self managedObjectContext];

        NSFetchRequest * fetch = [[NSFetchRequest alloc] init];

       [fetch setEntity:[NSEntityDescription entityForName:@"Clubs" inManagedObjectContext: context]];
       [fetch setPredicate:[NSPredicate predicateWithFormat:@"(clubid
 contains[cd] %@)",clubId]];

       NSArray * records = [context executeFetchRequest:fetch error:&error];
           for (NSManagedObject * record in records) {
                [context deleteObject:record];
            }
       [context save:&error];
    }
于 2013-10-07T04:56:02.080 回答
1

没有用于删除与谓词匹配的一组对象的批处理 Core Data 方法。您必须通过遍历结果来检索和删除它们。

NSManagedObjectContext *moc = // get the managed object context
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Clubs"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"clubid == 120"];
NSError *error = nil;
NSArray *entitiesToDelete = [moc executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *entity in entitiesToDelete) {
    [moc deleteObject:entity];
}
[moc save:&error];
if (error) {
    // handle error
}
于 2013-10-07T04:58:00.467 回答