ios - 如何删除具有用户定义 id 的 CoreData 对象?

标签 ios objective-c core-data

我有这样的对象

@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

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

假设有20个俱乐部被添加到CoreData中,我只想删除这些clubid为120的对象。

谢谢

最佳答案

    -(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];
    }

关于ios - 如何删除具有用户定义 id 的 CoreData 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19217269/

相关文章:

ios - 如何检测用户点击了 UIMenuController 中的格式化按钮?

ios - UIButton 不在 UIWindow 的 subview 上工作

objective-c - 为什么我在 Xcode 的设备菜单中没有任何配置文件?如何解决这个问题?

ios - 读取 NSFetchedResultsController 的 fetchedObjects 属性时出现 EXC_BAD_ACCESS 错误(仅在模拟器上)

json - 如何处理 JSON 中的多对多关系?

ios - NSExpression 使用 sqrt : function results in NSInvalidArgumentException

ios - 导航 Controller 弹出 subview 后,强制 parentView 刷新 View

objective-c - AVAudiosession播放iTunes库音乐吗?

ios - 来自实用程序类的 sharedInstance 的 presentViewController

iphone - ASIHTTPRequest 多个下载控制,如暂停、恢复