我试图在核心数据中创建一个像文件夹一样的实体,就像在 Mac 上基本上可以在其中放置对象和另一个文件夹一样。目前我有文件夹,单个书签(另一个实体)和书签可以在文件夹中,但我不能让文件夹进入另一个文件夹。
这是我的 NSFetchedResultsController,
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Bookmark" inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];
[NSFetchedResultsController deleteCacheWithName:@"Folder"];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"folder == %@", self.folder];
[fetchRequest setPredicate:pred];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.context sectionNameKeyPath:nil cacheName:@"Folder"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
如果 NSEntityDescription 实体是NSEntityDescription *entity = [NSEntityDescription entityForName:@"Folder" inManagedObjectContext:self.context];
它会因错误而崩溃,
*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“在实体中找不到密钥路径文件夹”。
我怎样才能解决这个问题?我被这个问题难住了。
Folder实体与书签实体是一对多的关系,书签实体有以下属性,title,url。Folder 有以下属性,title、displayOrder、isFolder。