iphone - 核心数据 - 不同的结果在 fetchedResultsController 中不起作用

标签 iphone core-data

我花了几个小时试图弄清楚以下问题......

我的 iPhone 应用程序中有以下核心数据 fetchResultsController,尽管在我的代码中设置了以下内容,但它不会返回一组不同的值...

// Only distinct values
[fetchRequest setReturnsDistinctResults:YES];

以下是整个fetchResultController...

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

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

[fetchRequest setEntity:[NSEntityDescription entityForName:@"CardMessage"inManagedObjectContext:self.managedObjectContext]];

// Set the properties to fetch
NSArray *propertiesToFetch = [[NSArray alloc] initWithObjects:@"category", nil];
[fetchRequest setPropertiesToFetch:propertiesToFetch];

// Only distinct values
[fetchRequest setReturnsDistinctResults:YES];

// Order the output
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"category" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc]
                        initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext
                        sectionNameKeyPath:nil
                        cacheName:@"Master"];

aFetchedResultsController.delegate = self;

self.fetchedResultsController = aFetchedResultsController;

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;
}    

最佳答案

您的 propertiesToFetch 数组包含一个字符串。它应该包含一个NSPropertyDescription。 (在本例中,它将是其子类,NSAttributeDescription。)

NSArray *propertiesToFetch = [[NSArray alloc] initWithObjects:
   [entity.propertiesByName objectForKey:@"category"], 
   nil];

(现代 Objective-C)

NSArray *propertiesToFetch = @[entity.propertiesByName[@"category"]]; 

如果您仍然得到重复条目,只需使用 NSSet 来获取唯一值。

NSSet *uniqueProperties = [NSSet setWithArray:resultsArray]; 

关于iphone - 核心数据 - 不同的结果在 fetchedResultsController 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9546050/

相关文章:

iphone - 蓝牙警告 Xcode?

iphone - 在没有支持变量的情况下在 Xcode 调试器中查看合成属性的值

iphone - 将图像文件上传到 Web 表单

objective-c - 正在跳过详细 View Controller 中的第一个方法

objective-c - 核心数据 : Observing changes in related entities

ios - 具有多个 NSDateFormatter 的 Swift 代码 - 优化

iPhone——当 alpha 设置为零时,为什么 UIViews 上的 TouchBegan 不触发?

javascript - block :hover on link for iPad

objective-c - MP4作为外部数据引用存储在CoreData中...如何获取路径?

ios - 使用 NSUndoManager 强制保存 NSManagedObject