我有一个使用 CoreDataTableView 的 fetchedResultsController,我想从中检索信息,问题是我没有从 fetchedresultsController 中获取任何值,我相信这些值存储在 CoreDataProperly 中,但我认为我没有正确检索它们。我有一个单独的文件来存储所有数据,并且我有一个应该传回 NSManagedObject 上下文的类方法,我认为问题可能出在此处。我正在存储 NSDictionary 的某些值,例如标题和 id 以及图像。这是代码
#import "FlickrVacationViewController.h"
#import "FlickrCoreDataProcess.h"
#import "VacationPhoto+Flickr.h"
@interface FlickrVacationViewController ()
@end
@implementation FlickrVacationViewController
-(void) setupFetchedResultsController{
NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@"VacationPhoto"];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]];
//[FlickrCoreDataProcess shareManagedObject] should share the NSManagedObject
//performFetch gets called here
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:[FlickrCoreDataProcess shareManagedObject].managedObjectContext sectionNameKeyPath:nil cacheName:nil];
NSLog(@"Number of Objects = %i", [[self.fetchedResultsController fetchedObjects] count]);
//This returns 0
NSLog(@"temp array is %@",self.fetchedResultsController.fetchedObjects);
//Array is empty?
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupFetchedResultsController];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
//This is not called, I think since the fetchedResultsController doesnt have any values
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Vacation Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
VacationPhoto *vacationPhoto = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"text = %@", vacationPhoto.title);
cell.textLabel.text = vacationPhoto.title;
return cell;
}
@end
这是存储数据的文件这里是传递NSManagedObject的方法
+(UIManagedDocument*) shareManagedObject{
NSURL* url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"Default Photo DataBase"];
static UIManagedDocument *doc = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
doc = [[UIManagedDocument alloc] initWithFileURL:url];
});
return doc;
}
这是我存储文件的方式
+(VacationPhoto*) photoWithFlickrInfo: (NSDictionary*) flickrInfo inManagedObjectContext: (NSManagedObjectContext*) context{
NSLog(@"Photo To Store =%@", flickrInfo);
VacationPhoto * photo = nil;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"VacationPhoto"];
request.predicate = [NSPredicate predicateWithFormat:@"uniqueID = %@", [flickrInfo objectForKey:FLICKR_PHOTO_ID]];
NSSortDescriptor * descriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:descriptor];
NSError *error = nil;
NSArray *matches = [context executeFetchRequest:request error:&error];
if (!matches || [matches count] > 1) {
// handle error
} else if ( [matches count] == 0){
photo = [NSEntityDescription insertNewObjectForEntityForName:@"VacationPhoto" inManagedObjectContext:context];
photo.placeName = [flickrInfo objectForKey:FLICKR_PHOTO_PLACE_NAME];
photo.title = [flickrInfo objectForKey:FLICKR_PHOTO_TITLE];
NSLog(@"title = %@", photo.title);
photo.uniqueID = [flickrInfo objectForKey:FLICKR_PHOTO_ID];
NSLog(@"ID = %@", photo.uniqueID);
photo.imgURL = [[FlickrFetcher urlForPhoto:flickrInfo format:FlickrPhotoFormatLarge] absoluteString];
} else {
photo = [matches lastObject];
}
return photo;
}
可能没有提到这一点,但我首先创建了一个 UITableViewController,然后将其设为 CoreDataTableViewController 的子类,因此应该从中调用 performFetch 和其他类似的函数。