不调用 NSFetchedResultsControllerDelegate 委托方法
Posted
技术标签:
【中文标题】不调用 NSFetchedResultsControllerDelegate 委托方法【英文标题】:NSFetchedResultsControllerDelegate delegates methods are not called 【发布时间】:2016-06-05 23:01:17 【问题描述】:这是我的代码:
class FetchResultControllerDataProvider: NSObject, NSFetchedResultsControllerDelegate
private let fetchedResultController: NSFetchedResultsController
private var delegate: TodayViewController!
init(fetchedResultController: NSFetchedResultsController, delegate: TodayViewController)
self.fetchedResultController = fetchedResultController
self.delegate = delegate
super.init()
self.fetchedResultController.delegate = self
try! self.fetchedResultController.performFetch()
func controllerWillChangeContent(controller: NSFetchedResultsController)
print("Controller will change")
func controllerDidChangeContent(controller: NSFetchedResultsController)
print("COntroller did changed")
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType)
print("Controller did change section")
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?)
print("Controller did change object at indexpath")
func loadData()
let request = NSFetchRequest(entityName: Transaction.entityName)
request.predicate = Transaction.defaultPredicate
request.sortDescriptors = []
request.returnsObjectsAsFaults = false
request.fetchBatchSize = 20
let frc = NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
let dataProvider = FetchResultControllerDataProvider(fetchedResultController: frc, delegate: self)
dataSource = TableViewDataSource(tableView: self.tableView, dataProvider: dataProvider, delegate: self)
当我添加数据时,FetchResultControllerDataProvider
中的任何方法都不会被调用。但我不知道为什么。
有人可以帮我吗?
编辑:
添加数据:
func addTransaction()
self.context.performChanges
Transaction.insert(self.context, name: self.name.text!, amount: Double(self.amount.text!)!, type: self.currentTypeTag)
public func saveOrRoleback() -> Bool
do
try save()
return true
catch
rollback()
return false
public func performChanges(block: () -> ())
performBlockAndWait
block()
self.saveOrRoleback()
所以这部分代码被调用了,并没有抛出错误。 但是我试图检查核心数据sql文件,它似乎是空的。所以没有添加数据。
【问题讨论】:
你试过删除 super.init() 吗? 您在代码的哪个位置添加数据?也许您需要包含更多代码。 什么是保留实例?是直接销毁吗? @Wain:由dataSource对象保留。 开启线程调试。展示您如何添加数据。显示一切运行的顺序。 【参考方案1】:添加数据时,您需要检查委托和 fetchedResultsController 对象是否仍然存在并正确填充。您可以尝试打印这些变量的值吗?
self.fetchedResultController
self.fetchedResultController.delegate
在您添加数据时,您的PersistentStore
中是否插入了数据?
【讨论】:
【参考方案2】: NSFetchRequest *fetchRequest11 = [[NSFetchRequest alloc] init];
NSEntityDescription *entity1 = [NSEntityDescription entityForName:@"RemainderDataBase" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest11 setEntity:entity1];
NSPredicate *p1=[NSPredicate predicateWithFormat:@"startdate < %@", [NSDate date]];
[fetchRequest11 setPredicate:p1];
NSError *fetchError;
NSError *error;
NSArray *fetchedProducts1=[managedObjectContext executeFetchRequest:fetchRequest11 error:&fetchError];
for (NSManagedObject *product in fetchedProducts1)
[managedObjectContext deleteObject:product];
[tableview reloadData];
[managedObjectContext save:&error];
[self.tableview reloadData];
self.FRC = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:nil];
self.FRC.delegate = self;
NSError *fetchingErr=nil;
if ([self.FRC performFetch:&fetchingErr])
NSLog(@"Succesfully Fetched data from RemainderDataBase");
else
NSLog(@"Failed To Fetch data from RemainderDataBase");
设置获取请求以检索您的数据
注意: 正确设置代表..
并在 ViewDidLoad 中添加这些行
id delegate = [[UIApplication sharedApplication] delegate];
self.managedObjectContext = [delegate managedObjectContext];
【讨论】:
以上是关于不调用 NSFetchedResultsControllerDelegate 委托方法的主要内容,如果未能解决你的问题,请参考以下文章
NSFetchedresultscontroller 与 tableview 插入新部分崩溃
更新相关 NSManagedObject 时更新 UITableView
NSFetchedResultsController:具有相同键的两个排序描述符不起作用。一个降序用于部分,另一个升序用于行