Store unsigned for a long time using master data

Variants of this question are asked here and here , but it seems that the question has not received a clear answer.

The problem I am facing is that the MPMediaLibrary environment keeps a reference to each MPMediaItem (music, video, podcast, ...) as a long time (uint64_t), but I cannot find a way to save this value using Core Data . Using Integer 64 as a data type doesn't seem like a trick, and I don't see an alternative.

+5
source share
2 answers

Since there is no support in Core Data unsigned long long, you may need to literally do the trick yourself.

, ... , uint64_t:

// header
@interface Event : NSManagedObject

@property (nonatomic, retain) NSData * timestamp;

- (void)setTimestampWithUInt64:(uint64_t)timestamp;
- (uint64_t)timestampUInt64;

@end


// implementation
@implementation Event

@dynamic timestamp;

- (void)setTimestampWithUInt64:(uint64_t)timestamp
{
    self.timestamp = [NSData dataWithBytes:&timestamp length:sizeof(timestamp)];
}

- (uint64_t)timestampUInt64
{
    uint64_t timestamp;
    [self.timestamp getBytes:&timestamp length:sizeof(timestamp)];
    return timestamp;
}

@end

, . :

Event *event = [NSEntityDescription insertNewObjectForEntityForName:@"Event"
                inManagedObjectContext:self.managedObjectContext];

uint64_t timestamp = 119143881477165;
NSLog(@"timestamp: %llu", timestamp);

[event setTimestampWithUInt64:timestamp];
[self.managedObjectContext save:nil];

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Event"];
Event *retrievedEvent = [[self.managedObjectContext executeFetchRequest:request
                           error:nil] lastObject];
NSLog(@"timestamp: %llu", [retrievedEvent timestampUInt64]);

:

2012-03-03 15:49:13.792 ulonglong[9672:207] timestamp: 119143881477165
2012-03-03 15:49:13.806 ulonglong[9672:207] timestamp: 119143881477165

, , , , timestamp .

+7

, , , . MPMediaLibrary NSString:

[NSString stringWithFormat:@"%@", [currentMediaItem valueForProperty:MPMediaEntityPropertyPersistentID]];
+1

All Articles