简体   繁体   English

核心数据无符号的长期最佳实践

[英]Core Data unsigned long long best practice

I need to store uint64_t number in Core Data property. 我需要在Core Data属性中存储uint64_t号。 I also need to use this property in predicate. 我还需要在谓词中使用此属性。
The problem is, Core Data only has Integer64 type, and I can't perform fetch request on that property. 问题是,Core Data仅具有Integer64类型,而我无法对该属性执行获取请求。
For example: 例如:

    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!  

As you can see here, I got no result when trying to fetch the same personId as the one I just created. 如您在这里看到的,尝试获取与我刚刚创建的personId相同的personId时,我没有任何结果。
I assume the problem is that Core Data store it as signed and so the value is different, but how can I solve that? 我认为问题在于核心数据将其存储为签名形式,因此值有所不同,但是我该如何解决呢?

And what about sorting? 那排序呢? suppose I want a fetch request that also sort by personId? 假设我想要一个也按personId排序的提取请求? whats happening now is that the big numbers become negative (because they are treated as signed) so they sorted to the beginning of the list. 现在发生的是,大数字变为负数(因为它们被视为带符号),因此将其排序到列表的开头。

So whats best practice when dealing with unsigned long long and Core Data? 那么,在处理无符号的long long和Core Data时,最佳实践是什么?

I would suggest storing these using the NSDecimalAttributeType . 我建议使用NSDecimalAttributeType存储这些。 That should be able to support any 64-bit number and sort correctly. 那应该能够支持任何64位数字并正确排序。

Alternatively, you could store these as binary data, since presumably they are more-or-less opaque identifiers. 或者,您可以将它们存储为二进制数据,因为它们大概是不透明的标识符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM