ami*_*jaj 2 core-data uisearchbar nspredicate nsfetchedresultscontroller ios
我的应用程序面临一个有趣的问题.首先,应用程序的前提很简单:一个表视图控制器,导航栏中有一个加号按钮; 用户按下加号,视图控制器以模态方式显示.用户添加名称,数量和事件,按下保存并通过NSFetchedResultsController和Core Data,该信息成功填充在表视图控制器中.
这非常有效.TableViewController也使用SearchBar,以便用户可以在表视图中搜索名称或事件.
问题是:
如果用户在表视图中搜索条目(并且未取消搜索),然后按添加按钮添加条目,填写信息并按保存,应用程序崩溃时出现以下错误:
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.'
Run Code Online (Sandbox Code Playgroud)
但是,如果用户确实从表视图中取消了搜索,然后添加了新条目,则不会崩溃.这是我需要解决的问题,因为在添加新条目之前,某些工作流可能看不到取消搜索的需要.
我在表视图中的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];
}
Run Code Online (Sandbox Code Playgroud)
我理解发生了什么,但我不完全确定如何解决它.错误似乎很常见,并且有很多修复,都与稍微调整谓词有关,但我不确定采用哪个方向.
我正在添加我的模型视图以获取更多信息.
对此的任何帮助都将受到大力赞赏.
编辑:我通过故事板而不是代码呈现视图控制器,但根据TheEye的评论,实际上只需清除代码中的搜索并手动呈现视图控制器可能更容易.我正在考虑将此作为一种前进方式进行探索.
如果"ANY"与一对一关系而不是多对多 关系一起使用,则会发生该错误.例如,如果"场合"是一对一的关系,那么你必须更换
@"ANY occasion.title CONTAINS[c] %@"
Run Code Online (Sandbox Code Playgroud)
通过
@"occasion.title CONTAINS[c] %@"
Run Code Online (Sandbox Code Playgroud)
在第二个谓词中.
| 归档时间: |
|
| 查看次数: |
905 次 |
| 最近记录: |