MagicalRecords fetch data from fetchedResultsController - core-data

I have add this fetchedResultsController to my UIViewController. I am using MagicalRecords.
This is my code:
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
_fetchedResultsController = [Artist fetchAllGroupedBy:nil withPredicate:nil sortedBy:#"artist_id" ascending:NO delegate:self];
return _fetchedResultsController;
}
But this code does not invoke.
I have UITableView in my UIViewController. I suppose that this method below should starts the method above but it does not:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id sectionInfo =
[[_fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
So the goal is fetch data using magical records and fetchedResultsController.
I can of course make something like -findAll but as I think fetchedResultsController will update data automatically when it will come instead of -findAll.

You have to declare a property (if you haven't done yet):
#property(strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
in the view controller,
and then access it via the property accessor and not the instance variable, e.g.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
self.fetchedResultsController calls the getter method fetchedResultsController, so that the FRC is created on the first call.
You should also set
_fetchedResultsController.delegate = self;
inside the getter method to enable automatic change tracking, and call
[_fetchedResultsController performFetch:&error];
for the initial fetch (unless MagicalRecord does that for you).

Related

updating NSManagedObject doesn't call NSFetchedResultsControllerDelegate using MagicalRecord

I have a model with this one to many relationShip:
Order -->> LineItem
I display LineItems in UITableViewCells:
I use UIPickerView for changing quantity of LineItems.
GOAL=> by changing picker value, subTotal be recalculated again.
the problem is here by updating lineItem, NSFetchedResultsController Delegate doesn't call (where I can reconfigure the cell again and display updated data). but when I update Order e.g set it as completed NSFetchedResultsController Delegate methods will be called.
why by updating lineItem doesn't affect delegates methods to be called?
I use magicalRecord and here is how I get NSFetchedResultsController
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
else
{
_fetchedResultsController = [Order fetchAllSortedBy:#"orderDate" ascending:YES withPredicate:nil groupBy:nil delegate:self];
}
return _fetchedResultsController;
}
the way I setup table view:
ConfigureCellBlock configureCell = ^(OrderDetailsCell *cell, LineItem *lineItem)
{
[cell configureForLineItem:lineItem];
};
//set fetchedresults controller delegate
Order *order = [[self.fetchedResultsController fetchedObjects] lastObject];
NSArray *lineItems = [order.lineItems allObjects];
self.ordersDataSource = [[ArrayDataSource alloc] initWithItems:lineItems cellIdentifier:#"lineItemCell" configureCellBlock:configureCell];
self.tableView.dataSource = self.ordersDataSource;
configuring cell:
- (void)configureForLineItem:(LineItem *)lineItem
{
self.menuItemName.text = lineItem.menuItemName;
self.price.text = [lineItem.unitPrice stringValue];
self.quantity.text = [lineItem.quantity stringValue];
self.totalPrice.text = [lineItem.totalPrice stringValue];
self.pickerController.model = lineItem;
self.picker.delegate = self.pickerController;
self.picker.dataSource = self.pickerController;
[self.picker setSelectedNumber:lineItem.quantity];
}
does fetching obj1 then updating obj3 cause the NSFRC delegate methods to be called?
The FRC will only observe changes to the objects that it is directly interested in, not any of the objects that they are related to.
You should configure your own observation, either directly with KVO or to the context being saved, and use that to trigger a UI refresh.

NSFetchedResultsController refresh for fetching new data

Please direct me to the right way.
I implemented this code to fetch my objects:
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSPredicate *predicate = nil;
if (self.selectedCategory)
{
predicate = [NSPredicate predicateWithFormat:#"ANY category_ids.category_id == %#", self.selectedCategory];
}
_fetchedResultsController = [EyeArtist fetchAllGroupedBy:nil withPredicate:predicate sortedBy:#"artist_id" ascending:NO delegate:self];
return _fetchedResultsController;
}
So when app run the at first time fetch works without predicate, so at second time I need new fetch with predicate.
I tap on the button and set string self.selectedCategory, but I don't know how to refetch data from - (NSFetchedResultsController *)fetchedResultsController;
So I suppose it has to be like execute new request for fetchedResultsController instance.
After changing the search criteria, you have to set the instance variable self.fetchedResultsController to nil,
so that the next call to the "lazy getter" function creates a new FRC with the
changed predicate. Something like this:
self.fetchedResultsController = nil;
[self.fetchedResultsController performFetch:&error];
[self.tableView reloadData];
This is the pattern I use for where a fetch controller needs a property:
- (void)setSelectedCategory:(id)selectedCategory{
if(selectedCategory == _selectedCategory){
return _selectedCategory
}
_selectedCategory = selectedCategory;
self.fetchedResultsController = nil;
if(self.isViewLoaded){
[self.tableView reloadData]; // but better to put this in an update views method that you can also call from viewDidLoad.
}
}
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
id selectedCategory = self.selectedCategory;
// Only need this if a category is required.
if(!selectedCategory){
return nil;
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY category_ids.category_id == %#", selectedCategory];
_fetchedResultsController = [EyeArtist fetchAllGroupedBy:nil withPredicate:predicate sortedBy:#"artist_id" ascending:NO delegate:self];
return _fetchedResultsController;
}

How to use groupBy with MagicalRecord?

I am stuck on how to use groupBy with MagicalRecord.
I have a list of countries with venues
Country -<< Venues
I need to group all the venues by the country and sort the countries by name.
But I am not sure how to do this with MagicalRecord.
I have tried to use a NSFetchedController but sometimes it crashes saying that the array is nil or 0 length.
Other times, it only ever sees 1 category when there are multiple.
Finally, I am not sure how to execute the fetch on an entity.
ie;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
_objects = [NSMutableArray array];
self.fetchedResultsController = [self fetchedResultsController];
[Venue performFetch:self.fetchedResultsController];
// At this point how do I make the Venue findAllSortedBy work on the performFetch?
_objects = [NSMutableArray arrayWithArray:[Venue findAllSortedBy:#"name" ascending:YES inContext:[NSManagedObjectContext defaultContext]]];
self.title = #"Venues";
}
- (NSFetchedResultsController *)fetchedResultsController {
if (!fetchedResultsController) {
fetchedResultsController = [Venue fetchAllSortedBy:#"name"
ascending:YES
withPredicate:nil
groupBy:#"country"
delegate:self];
}
return fetchedResultsController;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSLog(#"Section = %d", section);
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
NSLog(#"sectionInfo = %#", sectionInfo);
return #"Header";
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Venue *v = [_objects objectAtIndex:indexPath.row];
cell.textLabel.text = v.name;
}
I am not sure if I am doing this right.
The above will put everything in 1 country (when there are multiple) and the log will report;
CoreData: error: (NSFetchedResultsController) A section returned nil value for section
name key path 'country'. Objects will be placed in unnamed section
It seems not to see the different countries and I do not think I've done the GroupBy command correctly.
Thus, how do I do a GroupBy command with MagicalRecord?
Many thanks.
self.fetchedVenues = [Venue MR_fetchAllGroupedBy:#"country" withPredicate:nil sortedBy:#"name" ascending:YES];
This error is telling you that of all your Venue objects, there is at least one in your result set that does not have a value for "country". You need to verify that you are indeed filling in this field and saving it properly prior to fetching.
And FYI, in your viewDidLoad method, you don't need all that code. Simply do something like:
- (void) viewDidLoad;
{
[super viewDidLoad];
self.fetchedVenues = [Venue fetchAllSortedBy:#"name" ascending:YES withPredicate:nil groupBy:#"country" delegate:self];
}
fetchAllSortedBy... will perform the fetch for you, and log errors, etc. That is the point of a helper framework like MagicalRecord.

NSManagedObject passed to ViewController Does Reflect All Updates

In my first view controller what I'm doing is setting up a NSManagedObjectContext from a UIMangedDocument in my viewDidLoad
#property(strong, nonatomic) NSManagedObjectContext *managedObjectContext;
- (void)viewDidLoad
{
NSURL *filePath = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
filePath = [filePath URLByAppendingPathComponent:#"Locations"];
UIManagedDocument *document = [[UIManagedDocument alloc] initWithFileURL:filePath];
//Create if it doesn't exist
if (![[NSFileManager defaultManager] fileExistsAtPath:[filePath path]]) {
//Async save
[document saveToURL:filePath forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
if (success) {
self.managedObjectContext = document.managedObjectContext;
}
}];
} else if (document.documentState == UIDocumentStateClosed){
[document openWithCompletionHandler:^(BOOL success){
//Open it, don't need to refetch stuff
if (success) {
self.managedObjectContext = document.managedObjectContext;
}
}];
} else {
self.managedObjectContext = document.managedObjectContext;
}
}
Then I insert a new object via a category method on my NSMangedObject subclass
[Location createLocationWithName:#"Maui" inManagedObjectContext:self.managedObjectContext];
Which just calls this code
Location *location = [NSEntityDescription insertNewObjectForEntityForName:#"Location" inManagedObjectContext:managedObjectContext];
[managedObjectContext save:nil];
Now the problem I'm having is when I segue to a new ViewController that has a public NSManagedObjectContext property and set it to this managedObjectContext in prepareForSegue the NSFetchedResultsController in the destinationViewController doesn't pick up this change right away. After I navigate back a forth a few times it eventually sees the Location Maui I created above. Any ideas why inserting a new Object into the managedObjectContext and then passing it to another view controller doesn't reflect that change?
Any insight is greatly appreciated.
If you creation method contains the name you should at least also set this attribute (otherwise it will be lost). So second line of your creation implmentation:
location.name = name; // name is passed to the method
In order to ensure that the fetched results controller of the second view controller is updated immediately, you could set the cacheName to nil when creating the FRC. If you have lots of records and think you need the cache, you can do this in viewWillAppear:
[self.fetchedResultsController performFetch:&error];

How can I insert new objects at top of UITableView backed by Core Data/NSFetchedResultsController?

I have a tableview that is successfully incorporating an NSFetchedResultsController. However, I need the topmost cell in my tableview to read, "Add new object" and have UITableViewCellEditingStyleInsert instead of the default UITableViewCellEditingStyleDelete.
The FetchResultsController wants to check the managedObjectContext for objects--both to determine number of rows and to populate the table cells. The only way I can think to get around this is to create a dummy object, but I feel like there ought to be a more elegant solution.
UPDATE:
For those who might be curious as to what solution I ended up with, I decided to have my insert cell at the bottom, not the top. Here is the relevant code:
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
//self.clearsSelectionOnViewWillAppear = NO;
self.editing = YES;
self.tableView.allowsSelectionDuringEditing = YES;
self.tableView.delegate = self;
RubricAppDelegate *appDelegate = (RubricAppDelegate *)[[UIApplication sharedApplication] delegate];
managedObjectContext = [appDelegate managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"myClass" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"classID" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil cacheName:nil];
NSError *error;
[fetchedResultsController performFetch:&error];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(#"Number of sections = %d", [[fetchedResultsController sections] count]);
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
id <NSFetchedResultsSectionInfo> myClass = [[fetchedResultsController sections] objectAtIndex:section];
NSLog(#"Number of classes = %d", [myClass numberOfObjects]);
return ([[fetchedResultsController fetchedObjects] count] + 1);
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSLog(#"FRC count + 1 = %d", ([[fetchedResultsController fetchedObjects] count] + 1));
if (indexPath.row == ([[fetchedResultsController fetchedObjects] count])) {
cell.textLabel.text = #"Add New Class";
}
else {
myClass *theClass = [fetchedResultsController objectAtIndexPath:indexPath];
NSLog(#"Class name is: %#", theClass.classTitle);
cell.textLabel.text = theClass.classTitle;
}
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == [[fetchedResultsController fetchedObjects] count]) {
return UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
}
The result (with some junk data):
Now my only issue is getting the delete functions to work properly. You can follow my post on that issue here
Normally the add row is at the bottom.
You can accomplish this by changing the -tableView:numberOfRowsInSection: and the -tableView:cellForRowAtIndexPath: methods to adjust the cell count and adjust for it. So your -tableView:numberOfRowsInSection: would return N+1 and your -tableView:cellForRowAtIndexPath: would get object at N-1 unless N == 0 then it would return your "Add new object" cell.
There is no need to mess with the underlying Core Data elements as this is strictly a UI issue.
Update
But now I'm not sure how to return the count of my fetched objects (assuming that is what I used for "N" in your above answer). Also, wouldn't I want -tableView:cellForRowAtIndexPath to return my "Add new object" cell when the indexPath.row = (N + 1), not N = 0? I may be misunderstanding what "N" equates to, but I thought it just meant count of fetched objects.
Yes it is the count of actual objects.
You do want your -tableView:cellForRowAtIndexPath: to return a cell for your "Add new object" otherwise what is the point? You just want it to return a different type of cell.
All you are doing in this solution is adding a cell that is not part of the NSFetchedResultsController and then compensating for it when you are retrieving an actual object from the NSFetchedResultsController and when the user selects a cell.

Resources