简体   繁体   中英

NSFetchedResultsControllerDelegate delegates methods are not called

Here is my code:

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)
}

Neither methods from the FetchResultControllerDataProvider are called when I am adding data. But I can't figure it out why.

Someone can help me with this ?

edit:

Adding data:

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()
    }
}

So this part of the code is called, and there is no error thrown.
But I tried to check the core data sql file and it seems to be empty. So data is not added.

You need to check if the delegate and fetchedResultsController objects are still alive and populated properly when you are adding the data. Can you try to print values for these variables?

self.fetchedResultController

self.fetchedResultController.delegate

Is data being inserted in your PersistentStore when you add it?

    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");
    }

Set the Fetch Request to retrive your data

Note: set the delegates properly..

And add these line in ViewDidLoad

id delegate = [[UIApplication sharedApplication] delegate];
    self.managedObjectContext = [delegate managedObjectContext];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM