为什么此获取请求不返回任何内容

时间:2014-09-24 14:33:55

标签: ios objective-c nsmanagedobject nsfetchrequest

我正在尝试过滤一些托管对象。结果虽然是拉链。

过滤器是“名称”。 Name是此详细信息视图的根视图提供的元素。 名称记录确定:

[48582:2754928] name: To Be
[48582:2754928] predicate: name == @"name"

name = [self.detailItem valueForKey:@"name"];

NSError *error;
    if (![[self fetchedResultsController] performFetch:&error]) {
        // Update to handle the error appropriately.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }

和FetchResultsController如下:

 NSError *error = nil;
    name = [self.detailItem valueForKey:@"name"];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"VerbEntity" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    predicate = [NSPredicate predicateWithFormat:@"name == %@",@"name"];
    NSLog(@"name: %@", name);
    fetchRequest.predicate = predicate;
    NSLog(@"predicate: %@", predicate);

 // Create the sort descriptors array.
    NSArray *sortDescriptorArray = nil;

    //  Declare sort descriptors
    NSSortDescriptor *sortDescriptorPrimary = nil;




    sortDescriptorPrimary = [[NSSortDescriptor alloc] initWithKey:@"position" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];

    //  Set sort descriptor array
    sortDescriptorArray = @[sortDescriptorPrimary];

    //  Apply sort descriptors to fetch request
    [fetchRequest setSortDescriptors:sortDescriptorArray];

   NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

    NSLog(@"fetchedObjects count: %lu", (unsigned long)fetchedObjects.count);   
    _fetchedResultsController.delegate = self;   
    return _fetchedResultsController;
}

有什么突出的东西吗?谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

看起来你的_fetchedResultsController没有设置获取请求。也许还有不止这个代码片段显示......但这可能有效

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"VerbEntity"];
request.predicate = [NSPredicate predicateWithFormat:@"name == %@",@"name"];

// Note: Sort descriptors available depend on the backing store type.
NSSortDescriptor *sortDescriptorPrimary = [[NSSortDescriptor alloc] initWithKey:@"position"        
                                                                      ascending:YES 
                                                                       selector:@selector(localizedCaseInsensitiveCompare:)];

request.sortDescriptors = @[sortDescriptorPrimary];

// The request needs to be set on the controller.
NSFetchedResultsController *controller =
    [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                        managedObjectContext:self.managedObjectContext
                                          sectionNameKeyPath:nil
                                                   cacheName:nil];
// The controller needs to perform the fetch.
[controller performFetch:nil];  // Optionally pass in an NSError.
controller.delegate = self;
_fetchedResultsController = controller;
return controller;