gpt4 book ai didi

使用 Core Data 和 NSFetchedResultsControllers 使用 TableViewControllers 搜索后添加新条目时 iOS 应用程序崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:28:39 24 4
gpt4 key购买 nike

我的应用程序遇到了一个有趣的问题。首先,该应用程序的前提很简单:导航栏中带有加号按钮的表格 View Controller ;用户按下加号, View Controller 以模态方式出现。用户添加名称、金额和事件,按下保存并通过 NSFetchedResultsController 和核心数据,该信息成功填充回 TableView Controller 。

效果很好。 TableViewController 还使用 SearchBar,以便用户可以在 Table View 中搜索名称或事件。

问题是:

如果用户在表格 View 中搜索一个条目(并且没有取消搜索),然后按添加按钮添加一个条目,填写信息并按保存,应用程序崩溃并出现以下错误:

CoreData: error: Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  The left hand side for an ALL or ANY operator must be either an NSArray or an NSSet. with userInfo (null)
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The left hand side for an ALL or ANY operator must be either an NSArray or an NSSet.'

但是,如果用户实际上取消了 TableView 中的搜索,然后添加了一个新条目,它不会崩溃。这是我需要解决的问题,因为某些工作流程可能看不到在添加新条目之前取消搜索的必要性。

TableView 中的 NSFetchedResultsController 具有以下谓词:

if ([self.timelineSearchBar.text length] > 0) {
NSPredicate *predName = [NSPredicate predicateWithFormat:@"ANY whoBy.name CONTAINS[c] %@", self.timelineSearchBar.text];
NSPredicate *predOccasion = [NSPredicate predicateWithFormat:@"ANY occasion.title CONTAINS[c] %@", self.timelineSearchBar.text];

// creating the orCompoundPredicate for the two conditions
NSPredicate *compPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predName, predOccasion, predDate]];
[fetchRequest setPredicate:compPredicate];
}

我明白发生了什么,但我不完全确定如何着手解决它。该错误似乎很常见,并且有很多修复程序,所有这些都与稍微调整谓词有关,但我不确定要采取哪个方向。

我正在添加我的模型的 View 以获取更多信息。

enter image description here对此的任何帮助将不胜感激。

编辑:我通过 Storyboard而不是代码呈现 View Controller ,但根据 TheEye 的评论,实际上只清除代码中的搜索并手动呈现 View Controller 可能更容易。我正在考虑探索这一点作为前进的方向。

最佳答案

如果“ANY”用于对一关系而不是对多,则会发生该错误关系。例如,如果“场合”是一对一的关系,那么你必须替换

@"ANY occasion.title CONTAINS[c] %@"

通过

@"occasion.title CONTAINS[c] %@"

在第二个谓词中。

关于使用 Core Data 和 NSFetchedResultsControllers 使用 TableViewControllers 搜索后添加新条目时 iOS 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21880434/

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