我需要将 uint64_t
数字存储在 Core Data
属性中。我还需要在谓词中使用此属性。
问题是,Core Data 只有 Integer64 类型,我无法对该属性执行获取请求。
例如:
NSNumber *pid = @(9224992848802061623ULL); // some unsigned long long
// create a person with this id
Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.context];
person.personId = pid; // personId is core data NSNumber property typed integer 64
[self.context save:nil];
// now try to fetch this person we just added
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
request.predicate = [NSPredicate predicateWithFormat:@"personId == %@", pid];
NSArray *match = [self.context executeFetchRequest:request error:nil];
// NO RESULTS!
正如您在这里所看到的,当我尝试获取与我刚刚创建的 personId 相同的 personId 时,没有得到任何结果。
我认为问题是核心数据将其存储为签名的,因此值不同,但我该如何解决这个问题?
那么排序呢?假设我想要一个也按 personId 排序的获取请求?现在发生的情况是大数字变成负数(因为它们被视为有符号),因此它们被排序到列表的开头。
那么处理 unsigned long long 和 Core Data 时的最佳实践是什么?
最佳答案
我建议使用 NSDecimalAttributeType
存储它们。那应该能够支持任何 64 位数字并正确排序。
或者,您可以将它们存储为二进制数据,因为它们可能或多或少是不透明的标识符。
关于ios - 核心数据 unsigned long long 最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25997385/