我正在尝试删除所有对象,但我似乎无法使其正常工作。我知道这类问题还有其他问题,但它们没有帮助。我想要的是删除此方法中的section和fixture实体中的所有对象。
- (void)refresh:(UIRefreshControl *)refreshControl {
[refreshControl endRefreshing];
}
这是我用另一种方法保存和获取对象的地方。
NSManagedObjectContext *context = [self managedObjectContext];
for (int i=0; i <=fixtures.count-1; i++) {
fixture = [NSEntityDescription insertNewObjectForEntityForName:@"Fixture" inManagedObjectContext:context];
[fixture setValue:[NSString stringWithFormat:@"%@",[[fixtures objectAtIndex:i] objectForKey:@"date"]] forKey:@"date"];
[fixture setValue:[[fixtures objectAtIndex:i] objectForKey:@"time"] forKey:@"time"];
[fixture setValue:[[fixtures objectAtIndex:i] objectForKey:@"home"] forKey:@"home"];
[fixture setValue:[[fixtures objectAtIndex:i] objectForKey:@"away"] forKey:@"away"];
[fixture setValue:[[fixtures objectAtIndex:i] objectForKey:@"league"] forKey:@"league"];
}
for (int i=0; i <=sections.count-1; i++) {
lolSection = [NSEntityDescription insertNewObjectForEntityForName:@"Section" inManagedObjectContext:context];
[lolSection setValue:[NSString stringWithFormat:@"%@",[[sections objectAtIndex:i] objectForKey:@"date"]] forKey:@"date"];
}
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Fixture"];
self.theFixtures = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
NSFetchRequest *fetchRequest2 = [[NSFetchRequest alloc] initWithEntityName:@"Section"];
self.theSection= [[managedObjectContext executeFetchRequest:fetchRequest2 error:nil] mutableCopy];
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
NSArray *descriptors = [NSArray arrayWithObject: descriptor];
NSArray* reverseTheArray = [[self.theSection valueForKey:@"date"] sortedArrayUsingDescriptors:descriptors];
reversedArray = [[reverseTheArray reverseObjectEnumerator] allObjects];
答案 0 :(得分:0)
那么,到目前为止你尝试了什么?
根据文件。
删除托管对象非常简单。你只需发送它 托管对象上下文
deleteObject:
消息,传递对象 想删除作为参数。
换句话说,使用有效的上下文,你应该说
[aValidContext deleteObject:managedObjectYouWantToDelete];
关于您的问题,您需要
Fixture
个项目NSArray
)并执行删除例如
NSFetchRequest* request = // create the request here...
NSError *error;
NSArray *array = [aValidContext executeFetchRequest:request error:&error];
if (array == nil) {
// Deal with error...
} else {
// loop the array performing deleteObject
}
// save to persistent store
剩下的代码留给练习;)
修改1
再次阅读您的问题,Section
和Fixture
之间的关系是什么?如果它们是分隔的实体,则还应对Section
s执行先前的步骤。