Fetch Core Data object from UITableViews not made from Core Data

52 Views Asked by At

I'm trying to fetch a Core Data object and load it from a UITableView that is NOT made up from Core Data objects. It's simply a UITableView made with a predetermined array, but I want to load an object in Core Data with the same name as the cells detailTextLabel. I tried following this tutorial to load a specific value, but it's not working: It crashes or tells me my entity is nil, which it's not.

So basically I have a UITableView and it loads with this array:

array = @[@"publication 1",@"publication 2", etc]

Then, when a user selects the cell, I would like it to load a document based on the cells detailText, but it always loads whatever the objects indexPath is, instead of based on the cells detailText. So say in the main view controller where the predetermined array is loaded into the table view, I tap on Memo 24, but its index is (0,2) it will load an object, but it will load the object in the downloads entity that has the same indexPath (whatever publication is at 0,2) number as opposed to the Memo 24 title.

Here is my code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    [self loadPublicationLocallyWithPubAtIndex:indexPath withTitle:cell.detailTextLabel.text];
}

- (void)loadPublicationLocallyWithPubAtIndex:(NSIndexPath *)indexPath withTitle:(NSString *)pubNumber {
    NSManagedObjectContext *selectedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files)
    NSString *filePath;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"PDFs"];
    filePath = [filePath stringByAppendingPathComponent:[selectedObject valueForKey:@"puburl"]]; assert(filePath != nil); ;
    NSLog(@"Local path is : %@", filePath);
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        //File exists
        NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
        if (data)
        { 
            //load document
        }
    }
}

It works for all my view controllers that populate with Core Data, no matter what indexPath the cell is, it still retrieves the right object, so that leads me to believe it has something to do with the [self.fetchedResultsController objectAtIndex:]; part, so I even tried:

NSIndexPath *selectedItem = [self.fetchedResultsController indexPathForObject:cell.detailTextLabel.text];

in didSelectRowAtIndexPath:, but it doesn't work either.

How do you load an object based on a cells detailTextLabel as opposed to its indexPath?

1

There are 1 best solutions below

2
On BEST ANSWER

If you are populating your array from a static array, then you probably don't need to use a fetched results controller. You should instead just execute a fetch to get the relevant NSManagedObject. To ensure the fetch gets only the relevant object, use a predicate.

Suppose your NSManagedObjects are in a Publication Entity, and the relevant attribute is title. You should build a fetch like this:

NSManagedObjectContext *context = self.managedObjectContext; // or however your context is obtained
NSManagedObject *selectedObject;
NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Publication"]; // amend entity name as appropriate
fetch.predicate = [NSPredicate predicateWithFormat:@"title == %@",pubNumber];
NSError *error;
NSArray *results = [context executeFetchRequest:fetch error:&error];
if (results == nil) {
    NSLog(@"Fetch error %@, %@", error, [error userInfo]);
    abort();
} else {
    if ([results count] == 0) {
        NSLog(@"Publication not found");
    } else {
        // (note, should probably also check for count > 1 and handle accordingly)
        selectedObject = results[0];
        // then carry on with the remaining code from loadPublicationLocally....
        NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files)
        NSString *filePath;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"PDFs"];
        filePath = [filePath stringByAppendingPathComponent:[selectedObject valueForKey:@"puburl"]]; assert(filePath != nil); ;
        // ... etc etc.
    }
}