DatePicker停止CoreData工作 [英] DatePicker Stopping CoreData Work

查看:97
本文介绍了DatePicker停止CoreData工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序从UIDatePicker保存文本和日期,然后显示注释,如果你回到该日期在UIDatePicker。

I have an app which saves text and the date from a UIDatePicker and then shows that note if you got back into that date in the UIDatePicker.

它的工作伟大!只有我发现设置UIDatePicker日期到今天停止CoreData工作!

Its works great! Only I have found that setting the UIDatePicker date to today stops CoreData working!

它只有当我运行这个setDate行,它停止核心数据工作。应用程序运行良好,没有崩溃,它只是不保存任何数据。如果我评论这一行,它的作品的魅力。

Its only when I run this setDate line does it stop core data from working. The app runs fine without crashing, it just doesn't save any data. If I comment that line out, its works a charm. But I need to have the UIDatePicker on today when the app loads.

我在应用程序启动时使用这个:

I use this when the application starts:

NSDate *now = [[NSDate alloc] init];
[datePicker setDate:now];

这可以取得注释:

NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
        NSEntityDescription *testEntity = [NSEntityDescription entityForName:@"DatedText" inManagedObjectContext:self.managedObjectContext];
        [fetch setEntity:testEntity];
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"dateSaved == %@", datePicker.date];
        [fetch setPredicate:pred];

        NSError *fetchError = nil;
        NSArray *fetchedObjs = [self.managedObjectContext executeFetchRequest:fetch error:&fetchError];
        if (fetchError != nil) {
            NSLog(@"fetchError = %@, details = %@",fetchError,fetchError.userInfo);
        }
        noteTextView.text = [[fetchedObjs objectAtIndex:0] valueForKey:@"savedText"];

并保存注释:

NSManagedObject *newDatedText;
    newDatedText = [NSEntityDescription insertNewObjectForEntityForName:@"DatedText" inManagedObjectContext:self.managedObjectContext];
    [newDatedText setValue:noteTextView.text forKey:@"savedText"];
    [newDatedText setValue:datePicker.date forKey:@"dateSaved"];

    NSError *saveError = nil;
    [self.managedObjectContext save:&saveError];
    if (saveError != nil) {
        NSLog(@"[%@ saveContext] Error saving context: Error = %@, details = %@",[self class], saveError,saveError.userInfo);
    }


推荐答案

记住NSDate不仅保存DD / MM / YYYY,还有HH:MM:SS。

Remember NSDate saves not only DD/MM/YYYY but also HH:MM:SS.

我猜想当你从选择器选择DD / MM / YYYY时,时间为0:00:00,但在上面的情况,当你设置选择器日期到现在,你实际上是操作HH:MM:SS到别的东西(即使你没有手动看到它)。

At a guess I think when you pick a DD/MM/YYYY from the picker, it saves with a default time of 0:00:00 but in the case above when you set the picker date to now you are actually manipulating the HH:MM:SS to something else (even though you don't see it manually).

为了说明我想说什么,当你获取的是一个谓词(dateSaved == picker.date)时,它正在寻找格式为DD / MM / YYYY的日期00:00:00,为了争论,您可能已在DD / MM / YYYY 09:00:01保存。

To illustrate what I'm trying to say, when you fetch is with a predicate of (dateSaved == picker.date) it is looking for a date in the format DD/MM/YYYY 00:00:00 and for arguments sake you may have saved it on DD/MM/YYYY 09:00:01.

您需要对您的NSDate属性,如果你想让它工作。

You will need to do some formatting of your NSDate attribute if you want this to work.

这篇关于DatePicker停止CoreData工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆