如何搜索核心数据实体并删除某些对象
我不清楚如何从核心数据数据库中删除某些对象。我想我已经成功了,所以我可以找到这些对象,但不知道如何从核心数据中删除它们。在此示例中,我在实体新闻中搜索已过期的项目。我使用“expires”属性(int 32 unix 时间戳)并查看该数字是否小于当前的 unix 时间戳。不确定 NSPredicate 是否正确。
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"News" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
// Set up predicate here?
NSPredicate *pred = [NSPredicate predicateWithFormat:@"expires < %i", dateInt]; // dateInt is a unix time stamp for the current time
[request setPredicate:predicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"forename" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSError *error;
NSMutableArray *mutableFetchResults = [[[managedObjectContext executeFetchRequest:request error:&error] mutableCopy] autorelease];
[request release];
// delete the found objects here?
I'm unclear how to remove certain objects from Core Data database. I think I've got it working so I can find the objects, but don't know how to delete them from Core Data. In this example I'm searching the entity News for items which have expired. I use the 'expires' property (an int 32 unix time stamp) and see if the number is less than the current unix time stamp. Not sure if the NSPredicate is right in this.
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"News" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
// Set up predicate here?
NSPredicate *pred = [NSPredicate predicateWithFormat:@"expires < %i", dateInt]; // dateInt is a unix time stamp for the current time
[request setPredicate:predicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"forename" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSError *error;
NSMutableArray *mutableFetchResults = [[[managedObjectContext executeFetchRequest:request error:&error] mutableCopy] autorelease];
[request release];
// delete the found objects here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为每个要删除的对象调用
-[NSManagedObjectContext deleteObject:]
,然后提交更改。Call
-[NSManagedObjectContext deleteObject:]
for every object you want to delete, then commit the changes.您可以使用
NSBatchDeleteRequest
适用于iOS 9.0+
、macOS 10.11+
、tvOS 9.0+
、watchOS 2.0+
Swift 代码(来自上面的链接)
注意:
我发现下面关于
moc
的execute
的评论这意味着
moc
中任何未保存的数据都不会受到影响。即,如果您创建/更新了符合删除请求条件的实体,并且未在moc
上调用save
,则该对象将不会被删除。You can use
NSBatchDeleteRequest
available oniOS 9.0+
,macOS 10.11+
,tvOS 9.0+
,watchOS 2.0+
Swift code (from the above link)
NOTE:
I found below comment about
execute
ofmoc
Which means any unsaved data in
moc
won't be affected. i.e. if you've created/updated entity that falls in the delete request criteria and don't calledsave
onmoc
then that object won't be deleted.