NSFetchedResultsController 总是包含临时对象
NSFetchedResultsController always including temporary objects
我有一个 NSFetchedResultsController
设置如下:
let fetchRequest = NSFetchRequest(entityName: "Order")
fetchRequest.includesPendingChanges = false
fetchRequest.sortDescriptors = [
NSSortDescriptor(key: "status", ascending: false),
NSSortDescriptor(key: "date", ascending: false)]
self.fetchedResultsController = NSFetchedResultsController(
fetchRequest: fetchRequest,
managedObjectContext: DataStoreManager.sharedInstance.mainContext,
sectionNameKeyPath: "section",
cacheName: nil)
do {
try self.fetchedResultsController.performFetch()
} catch let error as NSError {
print(error)
}
问题是即使 includesPendingChanges
设置为 false,每次我在上下文中创建新对象(不保存)时它仍然调用 controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?)
。
还有什么我应该寻找的东西来避免这种情况吗?
推荐的方法是使用子上下文。父级应该是在获取的结果控制器中使用的主线程管理的对象上下文。
如果您保存,更改会立即"pushed"到主上下文,即获取的结果控制器。 (将主要上下文保存到持久存储中。)
如果不想保存,直接丢弃子上下文,不保存。获取的结果控制器永远不会知道它。
我有一个 NSFetchedResultsController
设置如下:
let fetchRequest = NSFetchRequest(entityName: "Order")
fetchRequest.includesPendingChanges = false
fetchRequest.sortDescriptors = [
NSSortDescriptor(key: "status", ascending: false),
NSSortDescriptor(key: "date", ascending: false)]
self.fetchedResultsController = NSFetchedResultsController(
fetchRequest: fetchRequest,
managedObjectContext: DataStoreManager.sharedInstance.mainContext,
sectionNameKeyPath: "section",
cacheName: nil)
do {
try self.fetchedResultsController.performFetch()
} catch let error as NSError {
print(error)
}
问题是即使 includesPendingChanges
设置为 false,每次我在上下文中创建新对象(不保存)时它仍然调用 controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?)
。
还有什么我应该寻找的东西来避免这种情况吗?
推荐的方法是使用子上下文。父级应该是在获取的结果控制器中使用的主线程管理的对象上下文。
如果您保存,更改会立即"pushed"到主上下文,即获取的结果控制器。 (将主要上下文保存到持久存储中。)
如果不想保存,直接丢弃子上下文,不保存。获取的结果控制器永远不会知道它。