在Realm DB中使用NSDate中的时间戳查询对象

时间:2017-06-21 13:05:07

标签: objective-c timestamp realm nsdate

我尝试从Realm获取具有特定时间戳的对象,但我没有得到任何结果。这是我使用的一段代码:

- (void) sendMediaChatMessage {
    //...
    NSLog(@"NSDate to string: %@", message.timeStamp);// is NSDate object
    NSDateFormatter *dateFormat = [ [ NSDateFormatter alloc ] init ];
    [ dateFormat setDateFormat:@"yyyyMMddHHmmssSSS" ];
    NSString *stringDate = [ NSString stringWithFormat:@"%@",[ dateFormat stringFromDate:message.timeStamp ] ];
    NSLog(@"Timestamp to string: %@", stringDate);
    //...
}

// When receiving chat message
- (void) updateChatStatusWithTimestamp: (NSDate*) timestamp withStatus: (ChatMessageStatus) state {

    NSDateFormatter *dateFormat = [ [ NSDateFormatter alloc ] init ];
    [ dateFormat setDateFormat:@"yyyyMMddHHmmssSSS" ];
    NSString *stringDate = [ NSString stringWithFormat:@"%@",[ dateFormat stringFromDate: timestamp ] ];
    NSLog(@"Reversed dateformat: %@", stringDate);
    NSLog(@"NSDate to string: %@", timestamp);

    NSPredicate *pred = [ NSPredicate predicateWithFormat:@"timeStamp == %@", timestamp ];
    RLMResults<RLMChatMessage *> *results = [ RLMChatMessage objectsWithPredicate: pred ];

    NSLog(@"Found chat items with the same timestamp: %lu", (unsigned long)results.count);
}

NSLog输出如下:

...发送时

2017-06-21 14:21:56.468224 sendChatMessage:

2017-06-21 14:21:56.468664 NSDate to string:Wed Jun 21 14:21:56 2017

2017-06-21 14:21:56.470802字符串的时间戳:20170621122156430

2017-06-21 14:21:56.471445留言:Hello world

2017-06-21 14:21:56.471922发送给用户:android

2017-06-21 14:21:56.586503从服务器收到Msg对象...

2017-06-21 14:21:56.586684 handleChatMessage

2017-06-21 14:21:56.586854消息类型:2

2017-06-21 14:21:56.586903聊天目的地:变形金刚

2017-06-21 14:21:56.586936时间戳:20170621122156430

2017-06-21 14:21:56.588515纬度:xx.906111

2017-06-21 14:21:56.588746经度:xx.476332

......收到时

2017-06-21 14:21:56.590504 Reversed dateformat:20170621142156430

2017-06-21 14:21:56.590602 NSDate to string:Wed Jun 21 14:21:56 2017

2017-06-21 14:21:56.591470发现时间戳相同的聊天项:0

我认为某些事情与时区有关。但我不知道如何解决这个问题。那么,Realm无法找到传递时间戳的对象的另一个原因是什么呢?

0 个答案:

没有答案