0

我有带有静态可编辑单元格的 UITableViewController。在我的 UITableViewController 中,我创建了 CoreData 对象,如下所示:

- (User*)user {
    if (_user == nil) {
        _user = [NSEntityDescription insertNewObjectForEntityForName:TABLE_USER inManagedObjectContext:self.managedObjectContext];
        NSError* error;
        [self.managedObjectContext save:&error];
        if (error != nil) {
            abort();
        }
    }
    return _user;
}

我想在 NSFetchedResultsController 中获取将在表中显示其字段的对象,然后保存该对象。我正在尝试做:

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:TABLE_USER_PROFILE inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:NO];
    NSArray *sortDescriptors = @[sortDescriptor];


    NSPredicate* pred = [NSPredicate predicateWithFormat:@"%@ = %@", TABLE_USER, self.user];
    [fetchRequest setPredicate:pred];

    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:TABLE_USER];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
        abort();
    }

    return _fetchedResultsController;
}

但它没有用。我得到空的 fetchedResultsController。

4

1 回答 1

1

如果self.user是 aNSManagedObject并且您查询一个User实体,您可以尝试编辑您的谓词,如下所示:

NSPredicate* pred = [NSPredicate predicateWithFormat:@"self == %@", self.user];

希望有帮助。

PS如果我的回答不起作用,请尝试提供一些其他详细信息(请参阅我的评论)

于 2012-08-28T12:35:14.857 回答