FetchedResultsController objects lose Relationships while scrolling - ios

I have a custom ViewController managing a TableView with a FetchedResultsController.
When my view is loaded everything seem to work fine (the cells contain the right data), however after scrolling the view the cells that get bind again with the data from the FetchedResultsController but the relationships of some objects are now set to nil.
controllerWillChangeContent and controllerDidChangeContent are not triggered during the scrolling.
This is the code in my view controller:
- (UITableViewCell *)tableView:(UITableView *)tb cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ClaimProductCell";
ClaimProductTableViewCell *cell = (ClaimProductTableViewCell*)[tb dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelItems = [[self productCellLoader] instantiateWithOwner:self options:nil];
cell = [topLevelItems objectAtIndex:0];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell*)cell atIndexPath:(NSIndexPath*)indexPath
{
ClaimProduct* claimProduct = (ClaimProduct*)[[self fetchedResultsController] objectAtIndexPath:indexPath];
ClaimProductTableViewCell* productCell = (ClaimProductTableViewCell*)cell;
productCell.indexPath = indexPath;
[productCell bindWithClaimProduct:claimProduct];
}
Here it follows the table view cell binding code:
- (void)bindWithClaimProduct:(ClaimProduct*)claimProduct
{
DocumentProduct* documentProduct = claimProduct.claimProductToDocumentProduct;
Product* product = documentProduct.documentProductToProduct;
self.addButton.hidden = YES;
self.productCode.text = product.code;
self.quantityLabel.text = [documentProduct.qty stringValue];
self.nameLabel.text = product.descriptionName;
self.documentCode.text = documentProduct.documentProductToDocument.code;
NSDate * date = documentProduct.documentProductToDocument.date;
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
self.documentDate.text = [NSString stringWithFormat:#"%ld/%ld/%ld", (long)[components day], (long)[components month], (long)[components year]];
// product image
NSString* imagePath = [ImagePathUtility imagePathFromImageName:product.image imageCode:product.code imageType:kImageProduct];
[self.productImageView setImageWithPath:imagePath placeholderName:kProductPlaceholderImage];
}
If I close and open again the app the view controller correctly shows the cells. If I scroll them I get the same problem again.
I am using Xcode 5 and working on an old project that does not use ARC.
[EDIT] This is NSFetchedResultsController lazy instantiation code:
- (NSFetchedResultsController*)fetchedResultsController
{
if (_fetchedResultsController)
return _fetchedResultsController;
NSManagedObjectContext* mainContext = [[AppDelegate sharedAppDelegate] managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"ClaimProduct" inManagedObjectContext:mainContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:#"claimProductToDocumentProduct.documentProductToProduct"]];
[fetchRequest setReturnsObjectsAsFaults:NO];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:#"claimProductToDocumentProduct.documentProductToProduct.code" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSPredicate* predicate = [NSPredicate predicateWithFormat:#"claimProductToClaim == %#", [self claim]];
[fetchRequest setPredicate:predicate];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:mainContext sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;
[fetchRequest release];
[sortDescriptor1 release];
[sortDescriptors release];
return _fetchedResultsController;
}
[EDIT 2] I noticed another error that arises when looping on the [self.fetchedresultscontroller fetchedObjects]. The object in the relationship seems to be non nil, however its properties are all nil. This is the portion of code:
- (void)updateTotals
{
NSArray * claimProducts = [self.fetchedResultsController fetchedObjects];
NSDecimalNumber * imponibile = [NSDecimalNumber decimalNumberWithString:#"0"];
NSDecimalNumber * lordo = [NSDecimalNumber decimalNumberWithString:#"0"];
for (ClaimProduct * claimProduct in claimProducts) {
if (![claimProduct.qty isEqualToNumber:#0]) {
//compute totals
NSDecimalNumber * unitPrice = [claimProduct.claimProductToDocumentProduct.price decimalNumberByDividingBy:claimProduct.claimProductToDocumentProduct.qty];
NSDecimalNumber * claimProductPrice = [unitPrice decimalNumberByMultiplyingBy:claimProduct.qty];
imponibile = [imponibile decimalNumberByAdding:claimProductPrice];
NSDecimalNumber * unitPriceLordo = [claimProduct.claimProductToDocumentProduct.totalprice decimalNumberByDividingBy:claimProduct.claimProductToDocumentProduct.qty];
NSDecimalNumber * claimProductPriceLordo = [unitPriceLordo decimalNumberByMultiplyingBy:claimProduct.qty];
lordo = [lordo decimalNumberByAdding:claimProductPriceLordo];
}
}
...
}
Here the claimProductToDocumentProduct relationship is not nil, however sometimes it happens that its properties are nil and I get a EXC_BAD_ACCESS in decimalNumberByAdding.

Related

Core Data Entity Search Using NSPredicate

I have an app that saves data to a Core Data sql then view it in a tableView. And I'm trying to add a searchBar to search through the data.
I created two NSFetchedResultsController. One for getting entity data and the other for getting entity data after filtering using NSPredicate.
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
CoreDataStack *coreDataStack = [CoreDataStack DefaultStack];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"offer" inManagedObjectContext:coreDataStack.managedObjectContext];
[fetchRequest setEntity:entity];
fetchRequest.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"date" ascending:NO]];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:coreDataStack.managedObjectContext sectionNameKeyPath:#"sectionName" cacheName:nil];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
- (NSFetchedResultsController *)fetchedSearchResultsController{
if (_fetchedSearchResultsController != nil) {
return _fetchedSearchResultsController;
}
CoreDataStack *coreDataStack = [CoreDataStack DefaultStack];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"offer" inManagedObjectContext:coreDataStack.managedObjectContext];
[fetchRequest setEntity:entity];
fetchRequest.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"date" ascending:NO]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"location contains[c] %#", self.searchDisplayController.searchBar.text];
[fetchRequest setPredicate:predicate];
_fetchedSearchResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:coreDataStack.managedObjectContext sectionNameKeyPath:#"sectionName" cacheName:nil];
_fetchedSearchResultsController.delegate = self;
return _fetchedSearchResultsController;
}
And showing data in each cell by cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];//] forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Entry *entry = [self.fetchedResultsController objectAtIndexPath:indexPath];
Entry *searchEntry = [self.fetchedSearchResultsController objectAtIndexPath:indexPath];
NSString *recipe = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = searchEntry.location;
} else {
cell.textLabel.text = entry.location;
}
return cell;
}
But I get this error: *'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'*
When I comment [fetchRequest setPredicate:predicate]; out, I don't get the error anymore. So I'm assuming the error is coming from predicate but I don't know how to overcome it.
Entry *entry = [self.fetchedResultsController objectAtIndexPath:indexPath];
Entry *searchEntry = [self.fetchedSearchResultsController objectAtIndexPath:indexPath];
first check for entry and searchEntry wether returning null or not ?

Tableview crashes when I scroll up to the top and hold

I have a table view that is fed by core data. It works fine except when I scroll back to the top and hold it there. Then the app crashes.
Any suggestions?
Thanks
// The Model for this class.
//
// When it gets set, we create an NSFetchRequest to get all Photographers in the database associated with it.
// Then we hook that NSFetchRequest up to the table view using an NSFetchedResultsController.
- (void)setManagedObjectContext:(NSManagedObjectContext *)managedObjectContext
{
_managedObjectContext = managedObjectContext;
if (managedObjectContext) {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"TestResults"];
request.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"date" ascending:YES selector:#selector(localizedCaseInsensitiveCompare:)]];
request.predicate = nil; // all records
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
} else {
self.fetchedResultsController = nil;
}
}
#pragma mark - UITableViewDataSource
// Uses NSFetchedResultsController's objectAtIndexPath: to find the Photographer for this row in the table.
// Then uses that Photographer to set the cell up.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TestResults"];
TestResults *testResult = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self showTestResult:testResult];
// show traffic light color
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yy hh:mm a"];
NSString *theDate = [dateFormatter stringFromDate:testResult.date];
cell.textLabel.text = theDate;
cell.detailTextLabel.text = testResult.overallPassOrFail;
NSLog(#"overall: %#", testResult.overallPassOrFail);
NSLog(#"light color: %#", testResult.trafficLightColor);
// retrieve an image
if ([testResult.trafficLightColor isEqualToString:#"Red"]) {
self.lightImage = #"Red_Light";
} else if ([testResult.trafficLightColor isEqualToString:#"Yellow"]) {
self.lightImage = #"Yellow_Light";
} else {
self.lightImage = #"Green_Light";
}
NSString *imagefile = [[NSBundle mainBundle] pathForResource:self.lightImage ofType:#"png"];
self.imageOne = [[UIImage alloc] initWithContentsOfFile:imagefile];
cell.imageView.image = self.imageOne;
//
return cell;
}
You are calling a method that is not implemented, check your code ;)

Logic issues with numberOfItemsInSection:

I'm trying to make my own version of this tutorial here (UICollectionView inside UITableViewCell): http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell
I'm having difficulties getting the correct count of UICollectionViewCells in each UITableViewCell:
The first cell is correct, it should have two UICollectionViewCells inside the UICollectionView (which of course, is inside a UITableViewCell). The second is what is throwing me up. Every time I make a new save in Core Data, it will add it to the previous cells. The second cell should have one UICollectionViewCell, not two UICollectionViewCells.
Here's my code (the code for AFTableViewCell & AFIndexedCollectionView can be found on the above tutorial link):
The numberOfItemsInSection: for the UICollectionView embedded in the UITableViewCell:
- (NSInteger)collectionView:(AFIndexedCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSIndexPath *indexPath;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Group" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *urlDescriptor = [[NSSortDescriptor alloc] initWithKey:#"url" ascending:NO];
NSArray *sortDescriptors = nil;
sortDescriptors = [[NSArray alloc] initWithObjects:urlDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSArray *fetchedObjects = [[managedObjectContext executeFetchRequest:fetchRequest error:&error]objectAtIndex:indexPath.section];
NSString *temp = [fetchedObjects description];
NSArray *tempArg = [temp componentsSeparatedByString:#","];
return [tempArg count];
}
The numberOfSectionsInTableView: for the main UITableView:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
id <NSFetchedResultsSectionInfo> sectionInfo = nil;
NSIndexPath *section;
sectionInfo = [[fetchedResultsController sections] objectAtIndex:section.section];
return [sectionInfo numberOfObjects];
}
The NSFetchedResultsController:
- (NSFetchedResultsController *)fetchedResultsController
{
TBAppDelegate *delegate = (TBAppDelegate *)[[UIApplication sharedApplication] delegate];
self.managedObjectContext = delegate.managedObjectContext;
NSFetchRequest *fetchRequest = nil;
fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = nil;
entity = [NSEntityDescription entityForName:#"Group" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:INFINITY];
NSSortDescriptor *urlDescriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:NO];
NSArray *sortDescriptors = nil;
sortDescriptors = [[NSArray alloc] initWithObjects:urlDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *frc = nil;
frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:#"Root"];
[frc setDelegate:self];
[self setFetchedResultsController:frc];
return frc;
}
The UITableView cellForRowAtIndexpath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
AFTableViewCell *cell = (AFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[AFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
Thanks in advance.
Use
NSArray *fetchedObjects = [[managedObjectContext executeFetchRequest:fetchRequest error:&error]objectAtIndex:section];
instead of
NSArray *fetchedObjects = [[managedObjectContext executeFetchRequest:fetchRequest error:&error]objectAtIndex:indexPath.section];
in your - (NSInteger)collectionView:(AFIndexedCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section function on line 10
It seems the IndexPath local variable is not initialized in this function. You can get section from the parameter passed.
and you haven't initialized section local variable in your
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
function.
If you do not initialize this variables and use them, they return a garbage value or crashes the program. be careful in these kind of access.

How to know type of NSManagedObject in NSManagedContext

I am using NSFetchedResultsController to fetch data from Core Data and to show them in UITableView. My NSFetchedResultsController looks like this:
- (NSFetchedResultsController *) fetchedResultsController
{
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"PlanPreparation"
inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
NSPredicate *pred = [NSPredicate predicateWithFormat:#"plan_id == %#", _planCycleID];
[fetchRequest setPredicate:pred];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor, nil];
fetchRequest.sortDescriptors = sortDescriptors;
_fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;
// but if there is no results I am making another fetch by another type of NSManagedObject
if (_fetchedResultsController.fetchedObjects.count == 0)
{
NSFetchRequest *fetchRequest1 = [[NSFetchRequest alloc] init];
NSEntityDescription *entity1 = [NSEntityDescription entityForName:#"Preparation"
inManagedObjectContext:_context];
[fetchRequest1 setEntity:entity1];
NSPredicate *pred = [NSPredicate predicateWithFormat:#"ismer == %#", [NSNumber numberWithInt:1]];
[fetchRequest1 setPredicate:pred];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor, nil];
fetchRequest1.sortDescriptors = sortDescriptors;
_fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest1 managedObjectContext:_context sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;
//return _fetchedResultsController;
}
return _fetchedResultsController;
}
If I am not retreiving something after first attempt to fetch, I need to set another fetch. In -cellForRowAtIndexPath I need to show data depends on type of NSManagedObjectObject:
- (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];
}
if ([[self.fetchedResultsController objectAtIndexPath:indexPath] isKindOfClass:[PlanPreparation2 class]])
{
PlanPreparation2 *planDrug = (PlanPreparation2 *)[self.fetchedResultsController objectAtIndexPath:indexPath];
CDPreparation *prep = (CDPreparation *)[[PTDataManager sharedManager]getDrugByID:planDrug.guid];
cell.textLabel.text = [NSString stringWithFormat:#"%#", prep.name];
}
else
{
CDPreparation *drug = (CDPreparation *)[self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:#"%#", drug.name];
}
return cell;
}
but logging shows that -isKindOfClass: don't work here, I am not getting into quotes at all.
Question:
How can I find out type of NSManagedObjectObject?
as shown in the picture change the name and class in data model inspector of that entity
name and class field in data model inspector must be same as name of entity table.
and then after you can compare like
if([(NSManagedObject *)[self.fetchedResultsController objectAtIndexPath:indexPath] isKindOfClass:[Category class]])
as "category" is the class in my case you can choose your own as you write above [PlanPreparation2 class]
if still you face problem then comment below

Identify the array of arrays in NSFetchedResultsController

I have built some code from a tutorial I found in a book. It works and I am able to display my data from CoreData successfully in a tableView. I now want to identify the data/object that the fetchRequest returns. I feel like such a dummy because I cannot understand the syntax enough to isolate my object containing my array of data. This is a snippet of the code which I am having difficulty understanding:
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Sessions" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
//NSLog(#"Entity is set to: %#", entity);
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:#"refid" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
//[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:#"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Sessions *info = [_fetchedResultsController objectAtIndexPath:indexPath];
//Format cell data ready to be displayed
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EE, dd LLL yyyy"];
NSString *dateString = [dateFormat stringFromDate:info.date];
NSNumber *dist1Nbr = info.dist1;
int dist1Int = [dist1Nbr integerValue];
float distIntKorM = ([dist1Nbr integerValue])/1000;
NSString *dist1StrMeters = [[NSString alloc] initWithFormat:#"%i", dist1Int];
NSString *dist1StrKorM = [[NSString alloc] initWithFormat:#"%.01f", distIntKorM];
//Select image to display
if ([info.sport isEqualToString:#"Run"]) {
UIImage *image = [UIImage imageNamed:#"trainers-15x10.png"];
cell.imageView.image = image;
cell.textLabel.text = [[NSString alloc] initWithFormat:#"%#: (%#),", dateString, info.sport];
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:#"Type: %#, Dist: %#", info.sessiontype, dist1StrKorM];
} else if ([info.sport isEqualToString:#"Other"]) {
UIImage *image = [UIImage imageNamed:#"weights-15x10.png"];
cell.imageView.image = image;
cell.textLabel.text = [[NSString alloc] initWithFormat:#"%#: (%#),", dateString, info.sport];
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:#"Type: %#, Dist: %#", info.sessiontype, dist1StrKorM];
}
}
If anyone can help me it would be massively appreciated.
You can access an array of NSFetchedResultsSectionInfo objects by calling:
NSArray *sections = _fetchedResultsController.sections;
Each of these objects represents a section in your table. For a given section, you can do something like:
id<NSFetchedResultsSectionInfo>section = sections[0];
NSArray *objects = section.objects;
to access the managed objects in that section. Or, if you've got an index path, you can access the associated object directly by doing:
NSManagedObject *object = [_fetchedResultsController objectAtIndexPath:indexPath];
On in reverse:
NSIndexPath *indexPath = [_fetchedResultsController indexPathForObject:object];
Is this what you're looking for?
update
Forgot this one. As Juan suggested below, you can access all fetched objects with:
NSArray *allObjects = _fetchedResultsController.fetchedObjects;
From the code you posted the NSFetchedresultsController is going to return all objects of class Sessions ordered descending by key 'refid'. NSFetchedResultsCOntroller is used together with a UITableViewController to display the results of a fetch request to the Core Data storage.
However if you want just to obtain all the objects in a array without using the table view you could do the following, for example in viewWillAppear:
if (self.managedObjectContext) {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Sessions"];
request.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"refid" ascending:NO];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
[self.fetchedResultsController performfetch];
NSArray *results = [self.fetchedResultsController fetchedObjects]; // an array of Sessions objects ordered descending by refid key
for (Sessions *sessions in results) {
NSLog(#"Sessions = %#", sessions);
}
}
You have to make sure that the NSManagedContext is properly initialized before or nothing will be displayed.

Resources