我正在尝试使用核心数据运行的测试应用程序,但此时我还没有进一步,我希望有人可以帮助我。
我正在创建一个应用来存储客户端及其项目。我使用核心数据与Big Nerd Ranch应用程序作为示例。此应用使用核心数据。
我想要完成的是您可以从客户端删除项目。只有当我从具有多个项目的客户端删除项目时,我的程序才会出错。
正如您在底部的日志文件中看到的,删除后,我的方法
-(NSArray *)relatedProjects:(Client *)client
不再包含任何项目。在
-(void)removeProject:(Project *)project
日志显示2个条目。
我正在使用数据存储区:
-(NSArray *)relatedProjects:(Client *)client; {
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *e = [[model entitiesByName] objectForKey:@"Project"];
[request setEntity:e];
// Check if client is related to Project
[request setPredicate: [NSPredicate predicateWithFormat:@"clients = %@", client.objectID]];
NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"project"
ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sd]];
NSError *error;
NSArray *result = [context executeFetchRequest:request error:&error];
if (!result) {
[NSException raise:@"Fetch failed"
format:@"Reason: %@", [error localizedDescription]];
}
relatedProjects = [[NSMutableArray alloc] initWithArray:result];
for (NSString *p in relatedProjects) {
NSLog(@"RELATEDPROJECTS %@", p );
}
if ([relatedProjects count] == 0) {
NSLog(@"relatedProjects is empty");
}
return relatedProjects;
}
-
-(void)removeProject:(Project *)project {
// remove from NSManagedObjectContext
[context deleteObject:project];
// remove from allProjects array
[allProjects removeObjectIdenticalTo:project];
NSLog(@"relatedprojects in remove %@", relatedProjects);
// remove from relatedProjects array
[relatedProjects removeObjectIdenticalTo:project];
NSLog(@"relatedprojects in AFTER remove %@", relatedProjects);
NSLog(@"removed project %@", [project project]);
}
和我的ViewController
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
BITDataStore *ds = [BITDataStore sharedStore];
NSArray *selectedProjects = [ds relatedProjects:client];
/* for (NSString *p in selectedProjects) {
NSLog(@"selectedProjects %@", p );
NSLog(@"IndexPath row %d", [indexPath row]);
}
*/
Project *pr = [selectedProjects objectAtIndex:[indexPath row]];
NSLog(@" te verwijderen project is %@ voor client %@", [pr project], [client name]);
[ds removeProject:pr];
// Delete the row from the data source
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; // lijkt dezelfde werking te hebben: [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
} }
我的日志
2013-12-10 14:11:49.863[37414:70b] relatedprojects in remove (
"<NSManagedObject: 0x8c83880> (entity: Project; id: 0x8c83260 <x-coredata://71BDF48B-7B2E-487D-A1E2-013904BB6757/Project/p4> ; data: {\n clients = \"0x8c75430 <x-coredata://71BDF48B-7B2E-487D-A1E2-013904BB6757/Client/p1>\";\n orderingValue = nil;\n project = 123;\n tasksProjects = \"<relationship fault: 0x8e71750 'tasksProjects'>\";\n})",
"<NSManagedObject: 0x8c83ac0> (entity: Project; id: 0x8c83270 <x-coredata://71BDF48B-7B2E-487D-A1E2-013904BB6757/Project/p1> ; data: {\n clients = \"0x8c75430 <x-coredata://71BDF48B-7B2E-487D-A1E2-013904BB6757/Client/p1>\";\n orderingValue = nil;\n project = ABC;\n tasksProjects = \"<relationship fault: 0x8c894f0 'tasksProjects'>\";\n})",
"<NSManagedObject: 0x8c83b20> (entity: Project; id: 0x8c83280 <x-coredata://71BDF48B-7B2E-487D-A1E2-013904BB6757/Project/p2> ; data: {\n clients = \"0x8c75430 <x-coredata://71BDF48B-7B2E-487D-A1E2-013904BB6757/Client/p1>\";\n orderingValue = nil;\n project = XYZ;\n tasksProjects = \"<relationship fault: 0x8d68f90 'tasksProjects'>\";\n})"
)
2013-12-10 14:11:49.863[37414:70b] relatedprojects in AFTER remove (
"<NSManagedObject: 0x8c83880> (entity: Project; id: 0x8c83260 <x-coredata://71BDF48B-7B2E-487D-A1E2-013904BB6757/Project/p4> ; data: {\n clients = \"0x8c75430 <x-coredata://71BDF48B-7B2E-487D-A1E2-013904BB6757/Client/p1>\";\n orderingValue = nil;\n project = 123;\n tasksProjects = \"<relationship fault: 0x8e71750 'tasksProjects'>\";\n})",
"<NSManagedObject: 0x8c83b20> (entity: Project; id: 0x8c83280 <x-coredata://71BDF48B-7B2E-487D-A1E2-013904BB6757/Project/p2> ; data: {\n clients = \"0x8c75430 <x-coredata://71BDF48B-7B2E-487D-A1E2-013904BB6757/Client/p1>\";\n orderingValue = nil;\n project = XYZ;\n tasksProjects = \"<relationship fault: 0x8d68f90 'tasksProjects'>\";\n})"
)
2013-12-10 14:11:49.864[37414:70b] removed project ABC
2013-12-10 14:11:49.865[37414:70b] related projects is empty
2013-12-10 14:11:49.866[37414:70b] **** client in numberOfRowsInSection is CLIENTX
2013-12-10 14:11:49.866[37414:70b] related projects is empty
我的模型按要求:
我的删除规则:
答案 0 :(得分:1)
我不确定我理解你的目标但是......如果我删除了一个客户端,我想删除他所有的项目。所以projects
的级联规则是可以的。相反,如果删除项目,则不应删除客户端。因此,clients
的nullify应该更好。
这对你有用吗?请参阅我之前的回答,因为对论证有深刻的解释。