我可以在创建FetchResultController对象后修改fetchRequest吗?

时间:2014-12-01 07:35:09

标签: ios core-data nsfetchrequest

抱歉,如果我无法完美解释。 我有一个fetchResultController getter。

如下 -

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

        }

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

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

        // Edit the sort key as appropriate.
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:keyColumnNamePid ascending:NO];
        NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

        [fetchRequest setSortDescriptors:sortDescriptors];


NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"myCache"];
    self.fetchedResultsController = aFetchedResultsController;

        self.fetchedResultsController.delegate = self;

        // Perform Fetch on Result Controller
 NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }        
            return _fetchedResultsController;
}

现在我想设置fetchRequest的一些谓词和属性。(仅当它需要时。) 我的意思是我做了另一种方法 -

-(NSFetchedResultsController *) fetchResultsControllerWithPredicate:(NSPredicate *) predicate{

    [self.fetchedResultsController.fetchRequest setPredicate:predicate];

    [NSFetchedResultsController deleteCacheWithName:@"myCache"];


    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}

现在当我打印它时,它返回_PFBatchFaultingArray如下: -

"<LibraryItem: 0xdc22150> (entity: LibraryItem; id: 0xdc208b0 <x-coredata://A1159A80-CA8B-4443-B96C-582BF0DC0290/LibraryItem/p332> ; data: <fault>)",
    "<LibraryItem: 0xdc224d0> (entity: LibraryItem; id: 0xdc1c590 <x-coredata://A1159A80-CA8B-4443-B96C-582BF0DC0290/LibraryItem/p290> ; data: <fault>)",
    "<LibraryItem: 0xdc22510> (entity: LibraryItem; id: 0xdc1c5a0 <x-coredata://A1159A80-CA8B-4443-B96C-582BF0DC0290/LibraryItem/p224> ; data:<fault>)"

问题是“数据:'&lt;'错误'&gt;' ”

此代码有什么问题? 我做错了吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在浪费了一整天后,我意识到了答案。 是的我们可以修改。

但是一旦您访问数据,您就会找到正确的数据。 因此,如果在打印时显示错误数据,请不要担心。

**When you just trying to print the values it will print a data : fault.
Because CoreData fetch data on Demand, means while you are printing it, it is partial Data.
When you access (you demand) the value, you find the complete data.**