Ios [NSFunctionExpression\u propertyType]:发送到实例的选择器无法识别

Ios [NSFunctionExpression\u propertyType]:发送到实例的选择器无法识别,ios,objective-c,core-data,icloud,Ios,Objective C,Core Data,Icloud,我的项目实现了与iCloud的核心数据同步。当我尝试删除重复项时,会出现以下异常: -[NSFunctionExpression\u propertyType]:发送到实例的无法识别的选择器 我已经了解到,这类问题的主要原因是在不同线程上使用相同的托管对象上下文。然而,在我的例子中,所有进程都是在主线程上完成的。我不知道如何解决这个问题 在初始化方法中,我添加了以下观察者: [[NSNotificationCenter defaultCenter] addObserverForName:NSPe

我的项目实现了与iCloud的核心数据同步。当我尝试删除重复项时,会出现以下异常:

-[NSFunctionExpression\u propertyType]:发送到实例的无法识别的选择器

我已经了解到,这类问题的主要原因是在不同线程上使用相同的托管对象上下文。然而,在我的例子中,所有进程都是在主线程上完成的。我不知道如何解决这个问题

在初始化方法中,我添加了以下观察者:

[[NSNotificationCenter defaultCenter] addObserverForName:NSPersistentStoreDidImportUbiquitousContentChangesNotification
                                                      object:self.managedObjectContext.persistentStoreCoordinator
                                                       queue:[NSOperationQueue mainQueue]
                                                  usingBlock:^(NSNotification *note) {
                                                      NSLog(@"NSPersistentStoreDidImportUbiquitousContentChangesNotification received");
                                                      [self.managedObjectContext performBlock:^{
                                                          [self.managedObjectContext mergeChangesFromContextDidSaveNotification:note];
                                                          [self filterDuplicates]; <-- Here I call method to call filtering duplicates
                                                      }];
                                                  }];
以及发生异常时的方法:

- (NSArray *)fetchAlbumsWithFetchRequest:(NSFetchRequest *)fetchRequest {
  Exception Here ---> NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                                               managedObjectContext:self.managedObjectContext
                                                                                                 sectionNameKeyPath:nil
                                                                                                          cacheName:nil];

    NSError *error = nil;
    if (![fetchedResultsController performFetch:&error]) {
        NSLog(@"Error when fetching Albums: %@", [error userInfo]);
        return nil;
    }

    return fetchedResultsController.fetchedObjects;
}
有什么帮助吗

编辑1:

我很肯定这与我们的要求有关。 NSFetchRequest实例已设置属性:

fetchRequest.propertiesToFetch=@[uniqueAttribute,countExpression]

这就是引起问题的
countExpression

在异常断点上,我记录了获取请求对象,并得到以下结果:

<NSFetchRequest: 0x10c58b4a0> (entity: SFGallery; predicate: ((null)); sortDescriptors: ((
    "(timestamp, descending, compare:)"
)); type: NSDictionaryResultType; includesPendingChanges: NO; propertiesToFetch: ((
    "(<NSAttributeDescription: 0x10c56e5c0>), name title, isOptional 1, isTransient 0, entity SFGallery, renamingIdentifier title, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null)",
    "count:(\"title\")"
)); propertiesToGroupBy: ((
    "(<NSAttributeDescription: 0x10c56e5c0>), name title, isOptional 1, isTransient 0, entity SFGallery, renamingIdentifier title, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null)"
)); )
编辑2:

当我得到豁免时:
[NSFunctionExpression\u propertyType]:无法识别的选择器发送到实例0x10c433d90

使用
po[0x10c433d90类]


我得到了
NSFunctionExpression

上面包含的代码部分来自苹果的示例代码。 资料来源如下:

我不敢相信苹果的工程师在提供示例之前不测试代码

首先,表达式应按以下方式定义:

NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:uniquePropertyKey];
NSExpression *countExpression = [NSExpression expressionForFunction: @"count:" arguments:@[keyPathExpression]];
在这种情况下,我很惊讶为什么这个实现不起作用:

NSExpression *countExpression = [NSExpression expressionWithFormat:@"count:(%@)", uniquePropertyKey];
也许有人知道为什么可以分享一些知识

还有苹果闪亮的花朵:

fetchRequest.propertiesToFetch = @[uniqueAttribute, countExpression];
应该是

fetchRequest.propertiesToFetch = @[uniqueAttribute, countExpressionDescription];

我希望这个答案能节省很多时间

查看异常堆栈,找出异常发生时的情况。在到达该点之前的某个地方,您传递了错误的对象类型(尽管它看起来可能不是错误的对象类型,因为指针类型与实际对象不匹配)。如果需要帮助,请发布异常堆栈。您可能希望尝试将输入问题序列的值的
[myPtr class]
值记录到日志中,以查看该类是否与您认为的一致。@HotLicks感谢您的重播。我已经为这个问题添加了额外的文档。顺便说一句,我已经打印了所有相关对象的类名称,它们看起来确实是正确的。我不喜欢核心数据,但我觉得奇怪的是,你没有在任何地方使用
countExpressionDescription
。我猜,
countExpression
有一个实际类型的NSFunctionExpression。谢谢,你帮我从很多桌面上解救了出来,试图弄清楚我基于Apple doc的代码为什么不能工作。这解决了几个问题。您还必须将自定义属性上的排序描述符添加到第二个dupe fetch请求中,否则循环将无法比较正确的对象,您将以大量重复项结束。
fetchRequest.propertiesToFetch = @[uniqueAttribute, countExpression];
fetchRequest.propertiesToFetch = @[uniqueAttribute, countExpressionDescription];