Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data - Disable loading from cache

I am working on a WatchKit Extension with using CoreData.

The CoreData StoreURL is the same like in the iOS App. Within the iOS App it's working like expected.

  1. I start the Watch-App in the Simulator.
  2. I change some data in the iOS App.
  3. The Watch-App is still loading the old data.

In the SQLite-File (which is used by both (App + WK-Extension)) there is the new data. So it seems that CoreData in the WatchKit-Extension is fetching the data from a cache.

How can I disable the cache or force loading the new data?

This is how I fetch the Data:

- (NSArray*)fetchAllActive:(NSError**)error
{
    NSFetchRequest* fetchRequest = [super fetchRequestForTemplate:@"AllActiveReminder" substitutionVariables:nil];
    NSArray* result = [super fetchWithRequest:fetchRequest error:error];
    return result;
}

A call of [managedObjectContext refreshObject: mergeChanges:] does only working in a for-loop for the WKTable.

Thanks!

like image 284
hfrahmann Avatar asked Mar 16 '23 10:03

hfrahmann


1 Answers

I came across the same problem when creating an Action extension that shared a Core Data store with it's containing app. The solution that I came across that works for me is to set the stalenessInterval on the Main Queue NSManagedObjectContext as follows inside of your extension:

objectContext.stalenessInterval = 0.0;

This tells the context in the extension to fetch new data every time and ignore the cache.

like image 179
excelzero Avatar answered Mar 23 '23 11:03

excelzero