实例化 NSFetchedResultsController 以未捕获的异常终止
Instantiating NSFetchedResultsController terminating with uncaught exception
我正在用抽象 class 的 fetchRequest 实例化 NSFetchedResultsController
:
private func setupFetchController() {
let fetchRequest : NSFetchRequest<NSFetchRequestResult> = SearchEntity.fetchRequest()
let fetchController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
self.fetchController = fetchController
}
SearchEntity
是 Person
和 Group
的抽象父 class。我用它来获取 2 个实体和 1 NSFetchedResultsController
。但是,调用此函数时应用程序崩溃:
libc++abi.dylib: terminating with uncaught exception of type NSException
我已将范围缩小到取消注释和注释 NSFetchedResultsController
的创建。我还有 2 个功能完全相同的功能。
我 doing/is 哪里出错了?
编辑:添加到这里。我可以通过使用 context.performFetch(...)
手动获取 SearchEntity
,这给了我正确的结果。但是,因此得名,我要搜索它,所以我需要能够有效地更新。
编辑2:
在其他地方运行的相同功能的示例:
private func setupFetchController() {
let fetchRequest : NSFetchRequest<NSFetchRequestResult> = Person.fetchRequest()
// Sort Persons
let sortDescriptor = NSSortDescriptor(key: "firstName", ascending: true)
let sortDescriptor2 = NSSortDescriptor(key: "lastName", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor, sortDescriptor2]
// Filter Persons (only iType = 1)
let predicate = NSPredicate(format: "iType == %i", 1)
fetchRequest.predicate = predicate
// Create the FetchController
let fetchController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: "sectionName", cacheName: nil)
self.fetchController = fetchController
}
您得到异常是因为至少需要一个排序描述符。
You typically create an instance of NSFetchedResultsController
as an
instance variable of a table view controller. When you initialize the
fetch results controller, you provide four parameters:
1) A fetch request. This must contain at least one sort descriptor to
order the results.
2) A managed object context. The controller uses this context to execute
the fetch request.
3) Optionally, a key path on result objects that returns the section
name. The controller uses the key path to split the results into
sections (passing nil indicates that the controller should generate a
single section).
4) Optionally, the name of the cache file the controller should use
(passing nil
prevents caching). Using a cache can avoid the overhead
of computing the section and index information.
我正在用抽象 class 的 fetchRequest 实例化 NSFetchedResultsController
:
private func setupFetchController() {
let fetchRequest : NSFetchRequest<NSFetchRequestResult> = SearchEntity.fetchRequest()
let fetchController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
self.fetchController = fetchController
}
SearchEntity
是 Person
和 Group
的抽象父 class。我用它来获取 2 个实体和 1 NSFetchedResultsController
。但是,调用此函数时应用程序崩溃:
libc++abi.dylib: terminating with uncaught exception of type NSException
我已将范围缩小到取消注释和注释 NSFetchedResultsController
的创建。我还有 2 个功能完全相同的功能。
我 doing/is 哪里出错了?
编辑:添加到这里。我可以通过使用 context.performFetch(...)
手动获取 SearchEntity
,这给了我正确的结果。但是,因此得名,我要搜索它,所以我需要能够有效地更新。
编辑2:
在其他地方运行的相同功能的示例:
private func setupFetchController() {
let fetchRequest : NSFetchRequest<NSFetchRequestResult> = Person.fetchRequest()
// Sort Persons
let sortDescriptor = NSSortDescriptor(key: "firstName", ascending: true)
let sortDescriptor2 = NSSortDescriptor(key: "lastName", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor, sortDescriptor2]
// Filter Persons (only iType = 1)
let predicate = NSPredicate(format: "iType == %i", 1)
fetchRequest.predicate = predicate
// Create the FetchController
let fetchController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: "sectionName", cacheName: nil)
self.fetchController = fetchController
}
您得到异常是因为至少需要一个排序描述符。
You typically create an instance of
NSFetchedResultsController
as an instance variable of a table view controller. When you initialize the fetch results controller, you provide four parameters:1) A fetch request. This must contain at least one sort descriptor to order the results.
2) A managed object context. The controller uses this context to execute the fetch request.
3) Optionally, a key path on result objects that returns the section name. The controller uses the key path to split the results into sections (passing nil indicates that the controller should generate a single section).
4) Optionally, the name of the cache file the controller should use (passing
nil
prevents caching). Using a cache can avoid the overhead of computing the section and index information.