gpt4 book ai didi

ios - 尝试从核心数据中的两个实体中删除数据

转载 作者:行者123 更新时间:2023-11-29 00:16:42 25 4
gpt4 key购买 nike

我尝试从两个实体中删除记录。核心数据有两个实体,名称 Student 和 Detail 都具有相反的关系。关系是

Student -> Detail:detail Detail -> Student:student

尝试从 TableView 中删除两个实体的记录。但是当我尝试删除时,只有实体 Student 从 delete 但从 detail Entity 无法删除。它向我显示了这个错误。

-[NSSet isSubsetOfSet:]: set argument is not an NSSet'

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
_mainContext = [appDelegate manageObjectContext];

[_mainContext deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
Detail *detailEntity = [self.fetchedResultsController objectAtIndexPath:indexPath];
Student *studentEntity = [self.fetchedResultsController objectAtIndexPath:indexPath];

NSMutableSet *mySet = [[NSMutableSet alloc] init];
[mySet removeObject: detailEntity];
[studentEntity removeDetail:mySet];
studentEntity.detail = detailEntity;

NSError *error = nil;
if (![_mainContext save:&error]) {
NSLog(@"Unresolve Error %@, %@", error, [error userInfo]);
abort();
}

[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

}
}

StudentCoredataclass.h

#import "Student+CoreDataClass.h"


NS_ASSUME_NONNULL_BEGIN

@interface Student (CoreDataProperties)

+ (NSFetchRequest<Student *> *)fetchRequest;

@property (nullable, nonatomic, copy) NSString *name;
@property (nullable, nonatomic, copy) NSString *study;
@property (nullable, nonatomic, copy) NSString *number;
@property (nullable, nonatomic, retain) NSSet<Detail *> *detail;

@end

@interface Student (CoreDataGeneratedAccessors)

- (void)addDetailObject:(Detail *)value;
- (void)removeDetailObject:(Detail *)value;
- (void)addDetail:(NSSet<Detail *> *)values;
- (void)removeDetail:(NSSet<Detail *> *)values;

@end
NS_ASSUME_NONNULL_END

DetailCoredataclass.h

#import "Detail+CoreDataClass.h"


NS_ASSUME_NONNULL_BEGIN

@interface Detail (CoreDataProperties)

+ (NSFetchRequest<Detail *> *)fetchRequest;

@property (nullable, nonatomic, copy) NSString *address;
@property (nullable, nonatomic, copy) NSString *contact;
@property (nullable, nonatomic, copy) NSString *email;
@property (nullable, nonatomic, copy) NSString *number;
@property (nullable, nonatomic, retain) Student *student;

@end

获取结果 Controller :

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
_mainContext = [appDelegate manageObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Student"];

// Add Sort Descriptors
[fetchRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO]]];

//[fetchRequest setRelationshipKeyPathsForPrefetching: @"detail"];
// Initialize Fetched Results Controller
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_mainContext sectionNameKeyPath:nil cacheName:nil];

// Configure Fetched Results Controller
[self.fetchedResultsController setDelegate:self];

// Perform Fetch
NSError *error = nil;
[self.fetchedResultsController performFetch:&error];

最佳答案

将您的代码替换为:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate manageObjectContext];
Student *studentEntity = [self.fetchedResultsController objectAtIndexPath:indexPath];
[context deleteObject:studentEntity];
NSError *error = nil;
[context save:&error];
}
}

接下来在模型中设置Delete Rule 以在删除学生时删除详细信息(反之亦然)。我不清楚为什么您将数据拆分为两个实体。

你不应该在这里删除 tableView 的 Cell。当您从 fetchedResultsController 获得委托(delegate)回调时,您应该将其删除。如果您还没有实现这些方法,那么您现在就实现吧。

关于ios - 尝试从核心数据中的两个实体中删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45164034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com