简体   繁体   English

Objective-C中的领域日期范围查询

[英]Realm date range query in Objective-C

I'm working with Realm and I have an array sorted by dates for all the entries of a specific card. 我正在使用Realm,我有一个按日期排序的特定卡的所有条目的数组。 I need to create a NSPredicate (or some other way) that will give me all the dates for that card in order for a specific month. 我需要创建一个NSPredicate(或其他方式),以便为该卡提供特定月份的所有日期。 My trouble is some months have different amounts of days in them. 我的麻烦是,其中几个月中的日子不同。

Here is what I have so far. 这是我到目前为止所拥有的。

// Filter specific card, create date and string for x axis
NSString *filteredName = [NSString stringWithFormat:@"%@", self.cardType];
NSDate *dateElement = [[NSDate alloc]init];
NSString *datedString = [[NSString alloc]initWithFormat:@""];

//Formatting datedString
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateFormat = @"MM/dd/YYYY";

NSSortDescriptor *dateSort = [[NSSortDescriptor alloc]initWithKey:@"date" ascending:YES];
NSArray *sortedArray = @[dateSort];

//self.orderHistory is all of the cards from the Realm database filtered down to a specific type of card.
for (Order *uniqueOrder in [self.orderHistory objectsWhere:@"name = [c] %@", filteredName]) {
    // [self.uniqueCardArray addObject:uniqueOrder];
    dateElement = uniqueOrder.date;
    datedString = [df stringFromDate:dateElement];
    [self.uniqueDatesForCardArray addObject:datedString];

}
[self.uniqueCardArray sortUsingDescriptor:sortedArray];

Code above gives me all the dates for a specific card I need the dates for a given month so I have tried this tacky solution but I would think there is something more elegant to say the least. 上面的代码为我提供了特定卡的所有日期,我需要给定月份的日期,因此我尝试了这种俗套的解决方案,但我认为至少可以说得有些优雅。

NSDate *todaysDate = [NSDate date];
NSTimeInterval secondsInMonth =- (60 * 60 * 24 * 365) / 12;
NSDate *dateOneMonthBack = [todaysDate dateByAddingTimeInterval:secondsInMonth];

NSPredicate *backDatesPred = [NSPredicate predicateWithFormat: @"date BETWEEN {%@, %@}", dateOneMonthBack, todaysDate];

Besides it being nasty code it will also take me back a month from the current date and not only back to the start of the month. 除了令人讨厌的代码外,它还需要我从当前日期返回一个月,而不仅是回到月初。 Been looking at the Realm documentation, NSCalendar stuff, and NSPredicate guide and now my head is swimming trying to count for all the possible problems that can go wrong with this. 一直在查看Realm文档,NSCalendar内容和NSPredicate指南,现在我的头都在努力计算可能导致此问题的所有可能问题。 Any advice or code snippets would be a great help. 任何建议或代码片段都将提供很大的帮助。

I'm not sure whether I understand your question correctly, the following code is what you want? 我不确定我是否正确理解您的问题,以下代码是您想要的吗?

To determine the date of the previous month, add NSDateComponet that has minus month to the date. 要确定上个月的日期,请在日期前加上负号的NSDateComponet

Realm supports BETWEEN query. 领域支持BETWEEN查询。 So you can just query by two days. 因此,您只需两天即可查询。

NSDate *todaysDate = [NSDate date];

NSDateComponents *comps = [NSDateComponents new];
comps.month = -1;

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *dateOneMonthBack = [calendar dateByAddingComponents:comps toDate:todaysDate options:kNilOptions];

RLMResults *filteredHistory = [self.orderHistory objectsWhere:@"name = [c] %@", filteredName];
RLMResults *rangedHistory = [filteredHistory objectsWhere:@"date BETWEEN {%@, %@}", dateOneMonthBack, todaysDate];

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

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