FetchedResultsController返回空数组

时间:2012-09-06 02:48:23

标签: xcode core-data nsfetchedresultscontroller nsmanagedobject cs193p

我有一个使用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和其他类似函数。

2 个答案:

答案 0 :(得分:0)

好像你忘了调用 - [NSManagedObjectContext save:]来实际保存你插入的对象。

答案 1 :(得分:0)

您似乎缺少对performFetch的调用以及告诉表每个部分中有多少行的数据源方法。