我必须在Realm
存储空间中存储大量数字,例如14000822124935161134
。目前我通过将它们的类型更改为string
来存储它们,如下所示,然后将其保存:
NSMutableDictionary *itemInsert = [item mutableCopy];
if([item valueForKey:@"timestamp"]) {
unsigned long long timestamp = [[item valueForKey:@"timestamp"] unsignedLongLongValue];
[itemInsert setObject:[NSString stringWithFormat:@"%llu", timestamp] forKey:@"timestamp"];
}
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[RMember createOrUpdateInRealm:realm withValue:itemInsert];
[realm commitWriteTransaction];
我的timestamp
的{{1}}属性定义如下:
RLMObject
@interface RMember : RLMObject
...
@property (nullable) NSString *timestamp;
...
@end
中的此类数据或更好的解决方案是否有合适的类型而不是string
?
答案 0 :(得分:1)
Realm支持long long
;它只是不支持unsigned
变体。
您可以简单地将值保存为long long
,并提供一个getter访问器,当从数据库中检索时,它会显式地将其强制转换回unsigned long long
。
@interface RMember : RLMObject
@property long long timestamp;
@end
unsigned long long timestamp = [[item valueForKey:@"timestamp"] unsignedLongLongValue];
RLMRealm *realm = [RLMRealm defaultRealm];
RMember *myObject = ...;
[realm transactionWithBlock:^{
myObject.timestamp = (long long)timestamp;
}];
unsigned long long savedTimestamp = (unsigned long long)myObject.timestamp;
NSLog(@"Saved timestamp is %llu", savedTimestamp);
在我的iPad Air上测试过它似乎按预期工作: