这是我的代码:
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文件,它似乎是空的。所以没有添加数据。
答案 0 :(得分:2)
您需要检查委托和fetchedResultsController对象是否仍处于活动状态,并在添加数据时正确填充。你能尝试打印这些变量的值吗?
self.fetchedResultController
self.fetchedResultController.delegate
添加数据时是否会在PersistentStore
中插入数据?
答案 1 :(得分:1)
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];