I have an app multi-language. The user inserts data based on the current language he/she chooses, for example: if the user chooses English, then that data he/she is inserting at the moment will be linked with the language English, if the user decides to change language, for example, go for the Portuguese language and then insert new record, the current record will be linked with the Portuguese language. So far, so good!
My problem is: I have a tableView where is loaded a list with the records the user inserted filtering by language (English, Portuguese, Italian, French etc..), I'm using core data for that (fetchedResultsController), but the problem is, the list doesn't update AT ALL!!! It only updates if I close the app and open it again. For example: I open the app with the English Language selected, then everything goes fine, but if I decide to change language to Portuguese, for example, then I come back to my list view and the listView still brings the list for the English language.
Looks like is something related to fetchedResultsController cache's name. In the apple's API reference it says: "If you are using a cache, you must call deleteCache(withName:) before changing any of the fetch request, its predicate, or its sort descriptors. You must not reuse the same fetched results controller for multiple queries unless you set the cacheName to nil" So I tried to delete the cache's name and create another one based on the current language, but no lucky! I've been stuck on the problems for 2 days and still didn't figure it out.
here is my fetchedResultsController
lazy var fetchedResultsController: NSFetchedResultsController<TranslationContainer> = {
let fetchRequest = NSFetchRequest<TranslationContainer>()
let entity = TranslationContainer.entity()
fetchRequest.entity = entity
let predicate = NSPredicate(format: "ANY dictionary.language == %@", "Portuguese")// it just update if I close the app and open
fetchRequest.predicate = predicate
let sortDescriptor = NSSortDescriptor(key: "word", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
fetchRequest.fetchBatchSize = 20
let fetchedResultsController = NSFetchedResultsController(
fetchRequest: fetchRequest,
managedObjectContext: self.managedObjectContext,
sectionNameKeyPath: nil,
cacheName: nil)// it's nil here now, but I tried to change the cache's name based on the current language, but it didn't work either
fetchedResultsController.delegate = self
return fetchedResultsController
}()