When should I use deleteCacheWithName with fetchedResultsController? - ios

When using core data + table views in combination with SearchDisplayController the predicate for the fetchedResultsController is changed such that only the subset of results confirming to the string in the search display are fetched.
The function shouldReloadDisplayForSearchString is called several times when the user types a search string. A common strategy seems to be to simply overwrite the current predicate for the fetched ResultsController with a new one. Apple suggests to invoke deleteCacheWithName when the search predicate is changed.
For me it seems to be a bit brutal to delete the cache every time. Is this the best practice for this case? Are there other strategies, such as instantiating a temporal fetchedResultsController for the search?
Thanks for Help!

I think that, as you said, it is a bit brutal and you'd better using a different request for search controller. I, for example, don't use a fetched results controller for search controller but a request to an array. While my fetched results controller is not modified.

Related

Realm over CoreData should I use NSFetchedResultController or a Dictionary?

I'm working on an app using Core Data and NSFetchedResultController but for many reasons I would like to switch it to use Realm.
I saw that there is a "Realm version" of the NSFetchedResultController on github but It wouldn't be compatible with my current code using Core Data.
The view controller is displaying a list of People from the same school, like an address book.
This list is a sublist of a people who studied in the same city.
So I was thinking to make 1 unique request to the database to retrieve the list of all people and then filter locally this list within 1 Dictionary per school [String: AnyObject] with String as Section name within the tableView and AnyObject an array of people.
first solution, make a NSFetchedResultController for each school with a predicate filtering the location. Then all delete actions etc.. are handled by the delegates -> this is not compatible with Realm
Create those dictionaries and update them for each actions... -> this works with Realm but it's very annoying to code.
Any better solution?
EDIT:
I need to clarify my request:
I'd like to write a class that inherit UITableViewController.
This class has a list of people sorted in the alphabetical order
The tableview has section with the first letter of their firstname
The tableview controller needs to handle updates of the data model (insert, update, delete)
As we might move from CoreData to Realm, I'd like to write code that is compatible with both so that I don't need to modify it again later. The only "smart" way I found to do it so far is to forget about the NSFetchedResultController and the RBQFetchedResultsController, because they are respectively linked to CoreData and Realm, and then use data structure like Dictionaries.
Just to clarify, you're creating a UITableView with multiple sections; 1 per school, and you want to sort a flat list of people in a Realm database into the table based on their school, correct?
If that Realm fetched results controller you mentioned (I'm guessing it's RBQFetchedResultsController) doesn't fit your app's architecture, then yeah, dictionaries would be the way to go, but it shouldn't hopefully be as 'manual' as you'd think.
The good thing about Realm Results objects are that they are 'live' in the sense that if a new item is added to Realm after the Results query was made, it'll be retroactively updated to include the new item. That being the case, as long as you're managing a dictionary of Results objects that each relate to fetching the people for each specific school, the only manual aspect would be managing the table sections themselves.
The only thing to be aware of is you'll need a mechanism to be notified when a new person has been added to a specific school (in order to know to refresh that section of the table view), but for now, it would be best if you did that in your own logic (Either through a callback block, or a delegate call).
On a sidenote, we're in the middle of adding a set of new APIs to make implementing native fetched results controller behaviour possible in Realm (i.e., automating the need to post a notification when a new object is added). We haven't got a proper release date confirmed yet, but it should be within the first quarter of 2016! :)

Pagination with Alamofire and realm.io

I have an api which could look like http://URL/news/:lastloaded/:size where lastloadedand size is the range of objects the api should return. This api returns a list of different news, which i want to show in a tableView. However in order to make it effective i wan't to make some kind of pagination, so that not all objects is loaded into the tableView. This i've achieved through simple variables like
let pageSize = 20
var lastLoadedPage = 0
however how do i make sure that the database in my case realm.io always is up to date with all the news from the api. I can easily change the api and add more parameters if it makes this easier? What is best practice? i'm using Alamofire and realm.io
Realm itself doesn't actually require pagination. The data is saved straight to disk, and then only the properties that are required are lazily paged in as they are called. As such, it's very memory-efficient, so much to the point where managing blocks of objects in memory (like pagination works) isn't necessary.
If you want to 'simulate' pagination with Realm, it's simply a matter of querying for all of the objects as a List, and then pulling out a sub-set of the objects you wish to display.
That all being said, it's probably still wise to paginate your calls to the web API so you don't needlessly download more news items than you require, but once they're downloaded and saved to Realm, you won't need to worry about any similar device-side logic. :)

Core Data--pass name of entity or reference to entire entity?

I'm making a simple bank account tracker, for self-instructional purposes. I'm using Core Data to store three entities, related as in the screenshot:
WMMGTransaction objects are simply stored as they are recorded, and extracted as needed to feed tableviews and detail views. This will be done via NSFetchedResultsController and a predicate. I'm using MagicalRecord to access Core Data, if that matters.
My question is this:
When I pass WMMGAccount data from one VC to another, such as when creating a new account, or when selecting one from a list (via delegation as a rule), does it matter if I pass a reference to the entire entity, or can I just use an NSString bearing the .name of the account and identify the account when required with a predicate and an NSFetchedResultsController? I guess this is a strategy question, and may generate discussion, rather than having a cut and dried answer, but I'm wrestling with it, so I thought I'd ask.
It sounds like you're asking if you should pass an object to the code that needs it, or if you should pass information that could be used to look up the same object again.
Unless you need to use the managed object on a different thread or queue, you should always pass the actual object. No sense re-fetching an object you already have. It's extra work and code complexity that (unless there are some unusual extenuating details you didn't mention) won't help in any way.
If you are needing to use the object on a different queue or thread, passing information that can be used to look it up is the correct approach. But in that case-- don't pass the value of one of the properties. Use the managed object ID.
Core Data won't force name values to be unique, while the object's managedObjectID is unique. It's also faster when retrieving the object, because you can use objectForID: or existingObjectForID: instead of performing a fetch.

What are some benefits of using NSFetchedResultsController other than loading subset of rows

We use NSFetchedResultsController for loading only sub set of rows rather than full data in the Table. Are there any unknown benefits of using it? Thanks.
You can fetch subset of rows using NSPredicate. So it's not really an advantage of FRC over plain NSFetchRequest with set predicate.
One of the biggest advantages is that a FRC can monitor storage changes for you and notify your provided delegate of said changes. Whether the data set has an entry added, removed or updated. See NSFetchedResultsControllerDelegate protocol reference.
It also has built-in caching mechanism. Although I haven't seen in my iOS-devs career people using it.
And last one that I can think of is that you are able to provide section's key path and you get "sectioned" data out of the box. With plain NSFetchRequest you'd have to section-ize data yourself.

How to directly edit PFQueryTableViewController's objects NSArray

simple question, as per the subject: i have no intention of uploading changes to a returned array of PFObjects, but want to purge the array of specific items. Given it is set to readonly, what is the best way to approach this while still using PFQueryTableViewController?
Or is it better to got with a regular tableviewcontroller and lose the benefits of parse's VC?
I am trying to wrap my head around getting the proper data i require but without proper SQL, it seems very very difficult. (lack of Distinct, grouping etc)
I would override the objectsDidLoad and objectAtIndexPath methods if you want to modify the returned results. See here for a better explanation: https://parse.com/questions/using-pfquerytableviewcontroller-for-uitableview-sections
It's just a good post on overriding the methods in PFQTVC to do different things with the data.

Resources