ios


Refresh NSFetchedResultController in Swift


I want to refresh NSFetchedResultController with different predicate if user clicks on an Action Sheet.
Here is the code:
lazy var fetchResultController: NSFetchedResultsController = {
let fetchRequest = NSFetchRequest(entityName: "Debt")
let filterText:String?
let ascending:Bool?
if NSUserDefaults.standardUserDefaults().objectForKey("filterType")?.stringValue == "name" {
filterText = "name"
ascending = true
}
else
{
filterText = "date"
ascending = true
}
let sortDescriptor = NSSortDescriptor(key: filterText, ascending: ascending!)
fetchRequest.sortDescriptors = [sortDescriptor]
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: CoreDataManager.sharedManager.managedObjectContext, sectionNameKeyPath: filterText, cacheName: nil)
fetchedResultsController.delegate = self
return fetchedResultsController
}()
#IBAction func didTouchUpInsideFilterButton(sender: UIBarButtonItem) {
let alertController = UIAlertController(title: "Filter By:", message: "", preferredStyle: .ActionSheet)
let nameAction = UIAlertAction(title: "Name", style: .Default) { (UIAlertAction) -> Void in
NSUserDefaults.standardUserDefaults().setObject("name", forKey: "filterType")
do {
try self.fetchResultController.performFetch()
} catch {
let fetchError = error as NSError
print("\(fetchError), \(fetchError.userInfo)")
}
};
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (UIAlertAction) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
};
alertController.addAction(nameAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
In the alertAction completion block after calling try self.fetchResultController.performFetch() method fetchResultController is not called again to update the predicate.
Please find a solution.
The chunk of code following the declaration of fetchResultController is for property initialization, and will only be run exactly one time, the first time the property is used. That means that that chunk of code that sets the predicate, etc., will not run when didTouchUpInsideFilterButton is called (unless that reference to self.fetchResultController is the first time fetchResultController is used, which seems unlikely).
You need to explicitly change fetchResultController outside of the initializer when you want to update it.

Related Links

Using NSURLProtocol with NSURLSession
Uploading file to Amazon S3 with KMS key in xcode
UIlabel multiline in UITableView
unable to set custom font in NSMutableAttributedString
How to make different search field for every sections in UITableview?
Im having trouble with using a custom navigation bar in SWIFT
Error playing embedded youtube video iOS 9
How to stop shooting with “NSTimer”
Warning: Attempt to present <GuessMeFinal> on <GuessMeFinal.ViewController> whose view is not in the window hierarchy -Swift
SpriteKit Gravity and Velocity have inconsistent speed on different devices
How bad is it to embed UIWebView in UIScrollView?
How to hide the toolbar when user taps an action in a CNContactViewController?
How Touch drag enter works?
How to fetch all contacts record in iOS 9 using Contacts Framework
Checking value of hundredths in Double
Why do I get a “HTTP load failed” exception after upgrading to XCode 7?

Categories

HOME
dynamic-programming
load
appium-ios
word-vba
libreoffice
cql3
normalization
weblogic11g
textbox
payment-processing
whitespace
range
ceph
windows-10-desktop
executable
plots.jl
smartsheet-c#-sdk-v2
aar
afnetworking-2
watchservice
servlet-filters
rational
freetype
textmatebundles
libpcap
man
google-distancematrix-api
ibm-datapower
centos6
icloud-drive
flexjs
c++builder-5
apscheduler
rdf4j
clappr
charts.js
dashboard-designer
increment
bezier
rights-management
sagepay
extjs4
csr
mdanalysis
project-centennial
tabview
distributed-lock
adal.js
psycopg2
multistore
ganglia
okular
harvest-scm
singlepage
android-alertdialog
django-errors
android-chips
proc
setwindowshookex
git-config
singly-linked-list
rras
flotr2
android-popupwindow
asp.net-webpages
pddl
dual-table
multer
laravel-forge
par
voting
nbug
smarty2
android-tablelayout
spiceworks
bit.ly
knockout-mvc
concurrentdictionary
syndication-feed
tinkerpop-frames
email-spam
magic-unipaas
modalpopupextender
execcommand
ioerror
nsimageview
mms-gateway
mosek
chromeless
haar-wavelet
hpple
noindex
nsinvocation
consistent-hashing
ramaze
measure
custom-tag
myisam
msn-messenger
crc-cards
shareware
mud
large-teams

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App