NSFetchedResultsController crashing on performFetch: when using a cache

后端 未结 12 1997
迷失自我
迷失自我 2020-12-23 14:05

I make use of NSFetchedResultsController to display a bunch of objects, which are sectioned using dates. On a fresh install, it all works perfectly and the objects are displ

相关标签:
12条回答
  • 2020-12-23 14:24

    I had a similar problem with one of my apps, when the Apple released the new iOS 4.0. Search:

    fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:nil];
    

    And set the value of the parameter cacheName to nil. It worked for me, hope it will for you. Let me know.

    0 讨论(0)
  • 2020-12-23 14:27

    If you're using the simulator, try resetting it--I'd guess you've changed your entity map and it's getting confused by a leftover cache. If not, you could try doing what the error says:

    - (void)applicationWillTerminate:(UIApplication *)application {
        [NSFetchedResultsController deleteCacheNamed:@"Events"];
        //etc
    }
    
    0 讨论(0)
  • 2020-12-23 14:27
    NSFetchedResultsController *fetched = [[NSFetchedResultsController alloc] initWithFetchRequest:fetch managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"day" cacheName:@"Events"];
    

    replace @"Events" with nil.

    0 讨论(0)
  • 2020-12-23 14:29

    I started getting the same error when I upgraded by MacBook Pro to Snow Leopard 10.6.4 and the latest SDK.

    As it turns out, many of us had been using code that wasn't in conformance with the rules, but we didn't know it because CoreData wasn't really behaving in accordance with its own rules.

    Specifically, when you fetch things, they get cached, and in 4.0, that cache isn't automatically purged in cases where it was purged in the earlier SDK.

    For me, the solution was simple. I just employed the class method that purges the caches. You can specify an individual entity, but I specify nil so it just does them all in this particular piece of start-up code:

    [NSFetchedResultsController deleteCacheWithName:nil];
    

    Suddenly, the little app I've worked on only to familiarize myself with CoreData is working again.

    0 讨论(0)
  • 2020-12-23 14:30

    The exception still occurs with Xcode 7 (beta 4):

    You have illegally mutated the NSFetchedResultsController's fetch request, its predicate, or its sort descriptor without either disabling caching or using +deleteCacheWithName:

    NOTE: This is with an unmodified Xcode "Template" Master-Detail iOS app with standard Xcode CoreData "boiler-plate" code created with Xcode 7 and using the latest (iOS 9) deployment target.

    I noticed it first when I restarted my app in the simulator. I had been starting and stopping the app several times via Xcode and then it happened; and it kept happening. I decided to do some experiments, and the results:

    • Every time I stopped the app in the simulator, I would get the exception on a subsequent launch.
    • Every time I stopped the app using the simulator's Home button, I was able to launch it again successfully.

    The issue can still be fixed as follows, using one or the other of the following methods:

    • In the AppDelegate's application didFinishLaunchingWithOptions method, add the following Swift NSFetchedResultsController.deleteCacheWithName(nil) or Objective-C [NSFetchedResultsController deleteCacheWithName:nil]; code. This clears out the corrupted cache.
    • In the Simulator, from the Simulator menu, select Reset Content and Settings. This fixes the problem, but you lose your test data.

    I also believe that this is an artifact of running via Xcode and stopping the app prior to it being able to clean up. I've not seen this in the actual device.

    0 讨论(0)
  • 2020-12-23 14:31

    Are you quitting the Simulator using the home button or by terminating the app in Xcode? Maybe the app isn't getting time to finish writing to the cache. Try using the home button to quit the app.

    0 讨论(0)
提交回复
热议问题