【问题标题】:Can I modify the fetchRequest after creating the FetchResultController Object.?创建 FetchResultController 对象后可以修改 fetchRequest 吗?
【发布时间】:2014-12-01 07:35:09
【问题描述】:

对不起,如果我不能完美地解释它。 我有一个 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>)"

问题是“数据:''”。

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

感谢您的帮助。

【问题讨论】:

    标签: ios core-data nsfetchrequest


    【解决方案1】:

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

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

    **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.** 
    

    【讨论】:

      猜你喜欢
      • 2014-08-03
      • 1970-01-01
      • 2012-10-11
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 2020-11-10
      相关资源
      最近更新 更多