gpt4 book ai didi

ios - 检索未包含在 CoreData 请求结果中的第二天

转载 作者:行者123 更新时间:2023-11-29 02:55:10 24 4
gpt4 key购买 nike

我想通过从今天开始搜索并排除核心数据请求提供的日期来获取下一个可用日期。

我的初始化代码,以及在同一个类中实现的帮助方法

+ (NSDate *)getDateByAddingDays:(NSInteger)days toDate:(NSDate *)date {
NSDate *newdate = [[NSDate alloc]init];
if (days != 0) {
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = days;
NSCalendar *calendar = [NSCalendar currentCalendar];
newdate = [calendar dateByAddingComponents:components toDate:date
options:0];
}
return newdate;
}

+ (void)myTroublesomeMethod {

// standard initialization of NSFetchRequest
NSManagedObjectContext *context = [(id)[[UIApplication sharedApplication] delegate] managedObjectContext];

NSDate *today = [NSDate date];

NSFetchRequest *request3 = [[NSFetchRequest alloc] init];
NSEntityDescription *chiusureDescription = [NSEntityDescription entityForName:@"Chiusure" inManagedObjectContext:context];
NSError *error;
[request3 setEntity:chiusureDescription];
NSArray *arrayChiusure = [context executeFetchRequest:request3 error:&error];

// I then extract the "data" attribute, that stores the date to be excluded

NSMutableArray *giorniChiusura = [[NSMutableArray alloc]init];
for (Chiusure *entity in arrayChiusure) {
[giorniChiusura addObject:(NSDate*) entity.data];
}

// and here is where the problem rises:

NSInteger days = 0;
NSDate *nextDate = [self getDateByAddingDays:days toDate:today];
if ([arrayChiusure containsObject:nextDate]) {

// HERE is the error, I suppose. nextDate is never contained in arrayChiusure, so the next while statement is an infinite loop.

while ([arrayChiusure containsObject:nextDate]) {
days += 1;
nextDate = [self getDateByAddingDays:days toDate:today];
}

// OMITTED: use of nextDate somewhere later in my code
}

我想到的替代方法:

  • 我知道我可以在 while 中创建一个新的 NSFetchRequest循环以检查建议的 nextDate 是否包含在Chiusure 实体数据集,但对我来说似乎有点过分了。

其他信息:

  • arrayChiusure 预计最多包含 10-20 个实体
  • 我认为该错误与错误比较有关(即在 [arrayChiusure containsObject:nextDate] 中),但我不明白为什么

感谢任何帮助:)提前致谢

最佳答案

正如评论中已经提到的,问题是 NSDate代表的不仅是天,而且时间也长达毫秒。因此你必须“规范化”日期首先表示当天的午夜(或存储在核心数据对象中的任何内容)。

<罢工>但是,有一个更简单的解决方案:您可以创建一个仅返回的获取请求商店中已存在的最新日期(请参阅 https://stackoverflow.com/a/18395185/1187415 作为示例),然后添加一天。

示例:

NSArray *arrayChiusure = [context executeFetchRequest:request3 error:&error];

// Extract "data" attribute into new array:
NSArray *giorniChiusura = [arrayChiusure valueForKey:@"data"];

// nextDate = "today at midnight":
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *today = [NSDate date];
NSDate *nextDate;
[calendar rangeOfUnit:NSDayCalendarUnit startDate:&nextDate interval:NULL forDate:today];

// Find next available day:
NSDateComponents *oneDay = [[NSDateComponents alloc]init];
oneDay.day = 1;
while ([giorniChiusura containsObject:nextDate]) {
nextDate = [calendar dateByAddingComponents:oneDay toDate:nextDate options:0];
}

关于ios - 检索未包含在 CoreData 请求结果中的第二天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23991992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com