0

我有一个独立的 CoreData 实体,其中包含大约 30 条记录。我必须定期删除记录并重新填充相关的 CoreTableViewController。我的 NSFetchRequest 没有返回任何记录。

但是 setupFetchedResultsController 中完全相同的 FetchRequest 确实会返回记录。

我确信我在这里遗漏了一些明显的东西,但还没有弄清楚是什么。有任何想法吗?

 - (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];
}

下面是调用方法:

 - (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];
}
}
4

1 回答 1

0

好吧,我不明白为什么这个特定的 fetch 请求不返回任何记录,但我采用了 Matthew Frederick 的问题解决方案here。能够覆盖记录并提供更优雅的解决方案。

如果有人感兴趣,将发布更多详细信息。

于 2013-08-16T20:10:36.947 回答