Question

I have a stand alone CoreData Entity that contains approximately 30 records. I periodically have to delete the records and repopulate the associated CoreTableViewController. My NSFetchRequest is not returning any records.

But the exact same FetchRequest in the setupFetchedResultsController does return records.

I am sure I am missing something obvious here, but have not figured out what. Any ideas?

 - (void)eraseFetchedResults:(NSManagedObjectContext *)myContext
 {
 NSFetchRequest *allAthletes = [NSFetchRequest fetchRequestWithEntityName:@"ResultsForAthleteSearch"];
allAthletes.sortDescriptors = [NSArray arrayWithObject:[
                NSSortDescriptor sortDescriptorWithKey:@"orderRecordsDownloaded"
                                             ascending:YES]];
// no predicate because we want ALL the Athletes

[allAthletes setIncludesPropertyValues:YES]; //only fetch the managedObjectID

NSError * error = nil;
NSArray * athletes = [myContext executeFetchRequest:allAthletes error:&error];
NSLog(@"NSArray athletes is %d", [athletes count]);
//error handling goes here
for (NSManagedObject * athlete in athletes) {
    [myContext deleteObject:athlete];
}
NSError *saveError = nil;
[myContext save:&saveError];

}


 - (void)setupFetchedResultsController // attaches an NSFetchRequest to this UITableViewController
 {
 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"ResultsForAthleteSearch"];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"orderRecordsDownloaded" ascending:YES]];
// no predicate because we want ALL the Athletes

self.fetchedResultsController = [[NSFetchedResultsController alloc]  initWithFetchRequest:request
                                                                      managedObjectContext:self.athleteSearchDatabase.managedObjectContext
                                                                       sectionNameKeyPath:nil
                                                                                cacheName:nil];
}

Here is the calling method:

 - (void)useDocument
 {
 NSLog(@"Does athleteSearchDatabase exist? %d", [[NSFileManager defaultManager] fileExistsAtPath:[self.athleteSearchDatabase.fileURL path]]);
NSString *checkName = _searchName;
if (![[NSFileManager defaultManager] fileExistsAtPath:[self.athleteSearchDatabase.fileURL path]]) {
    // does not exist on disk, so create it
    [self.athleteSearchDatabase saveToURL:self.athleteSearchDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
        [self setupFetchedResultsController];

        NSLog(@"checkName is %@ before calling fetchAthleteSearchResultsIntoDocument.",checkName);


        [self fetchAthleteSearchResultsIntoDocument:self.athleteSearchDatabase whereNameIs:checkName];
    }];
} else if (self.athleteSearchDatabase.documentState == UIDocumentStateClosed) {
    // exists on disk, but we need to open it
    [self eraseFetchedResults:self.athleteSearchDatabase.managedObjectContext];
    [self.athleteSearchDatabase openWithCompletionHandler:^(BOOL success) {

        [self setupFetchedResultsController];
        [self fetchAthleteSearchResultsIntoDocument:self.athleteSearchDatabase whereNameIs:checkName];
    }];
} else if (self.athleteSearchDatabase.documentState == UIDocumentStateNormal) {
    // already open and ready to use
    [self setupFetchedResultsController];
}
}
Was it helpful?

Solution

Well, I did not figure out why this particular fetch request return no records, but I employed Matthew Frederick's solution to question asked here. Was able to overwrite the records and came away with a more elegant solution.

Will post more details if anyone is interested.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top