2014-09-23 147 views
0

我需要在Core Data属性中存储uint64_t号码。我也需要在谓词中使用这个属性。
问题是,核心数据只有Integer64类型,并且我无法对该属性执行提取请求。
例如:核心数据无符号long long best practices

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正如我刚才创建的时候没有结果。
我假设问题在于Core Data将其存储为有符号值,因此值不同,但我该如何解决这个问题?

那么排序呢?假设我想要一个也由personId排序的提取请求?现在发生的事情是,大数变成负数(因为它们被视为签名),所以他们排序到列表的开头。

那么在处理unsigned long long和Core Data时最佳做法是什么?

回答

1

我建议使用NSDecimalAttributeType存储这些。这应该能够支持任何64位数字并正确排序。

或者,您可以将它们存储为二进制数据,因为它们可能是或多或少不透明的标识符。

+0

你可以解释什么时使用小数?它似乎工作,但我不明白为什么 – Eyal 2014-09-23 14:35:47

+0

@Eyal它将该值存储为一个NSDecimalNumber,其实质上是基数为10的数字与38位数的精度。所以,为64位无符号整型空间提供足够的空间。 – 2014-09-23 14:52:59