Eyal
Eyal

Reputation: 10828

Core Data unsigned long long best practice

I need to store uint64_t number in Core Data property. 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.
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.
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? 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?

Upvotes: 0

Views: 702

Answers (1)

Jesse Rusak
Jesse Rusak

Reputation: 57178

I would suggest storing these using the NSDecimalAttributeType. That should be able to support any 64-bit number and sort correctly.

Alternatively, you could store these as binary data, since presumably they are more-or-less opaque identifiers.

Upvotes: 1

Related Questions