Core Data iPhone - Save/Load depending on Date

爱⌒轻易说出口 提交于 2019-12-04 19:16:53

Allright, first of all, try to understand how core data works. I think you're pretty close, but programming is all about really knowing what's going on, instead of 'kinda' knowing what's going on. The Getting started with Core Data" guide is quite instructive.

Now about your code (I don't know you're exact data-model and surrounding code, so I'm kinda guessing here). I'm assuming you have a member called 'datePicker', which refers to the datepicker that you're using.

Allright, let me see if I can quickly fix some things in your code, but do be a bit careful. I haven't read through every line of your source code, and I haven't tried compiling it, so there will probably still be errors. In the end, you'll still a thorough understanding of the code.

-  (void)textViewDidEndEditing:(UITextView *)textView { 
    NSManagedObjectContext *context = [OrganizerAppDelegate managedObjectContext]; 
    NSManagedObject *note = [NSEntityDescription insertNewObjectForEntityForName:@"Notes" inManagedObjectContext:context]; 
    [note setValue:notesView.text forKey:@"text"]; 

    // Set the timestamp as well.
    [note setValue:datePicker.date forKey:@"timeStamp"];

    NSError *err=nil; if (![context save:&err]) { NSLog(@"Whoops, couldn't save: %@", [err localizedDescription]); } }

/* Beware, we're just storing every edit here, without every throwing anything away. We'll try to display just the latest note in the 'dateChanged', but you might want to delete any previous notes first.*/

/* I don't think this whole dissecting and generating a date is necessary */

- (void) dateChanged:(id)sender{

    //Load...
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Text" inManagedObjectContext:self.managedObjectContext];
    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
    [request setEntity:entityDescription];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"(timeStamp = %@)",datePicker.date];
    [request setPredicate:predicate]; //added this line later
    NSArray *array = [self.managedObjectContext executeFetchRequest:request error:&error];

    // Assuming that the last item in the array is the item that was added last. This might or might not be reasonable.
    notesView.text = [[array lastObject] text];

    [monTable reloadData];
    [tueTable reloadData];
    [wedTable reloadData];
    [thuTable reloadData];
    [friTable reloadData];
}
apoorv shah

When ever you add or edit any object , you should save that object in MOC(ManagedObjectContect).

Here is modified Code:

-(void)textViewDidEndEditing:(UITextView *)textView{
 NSManagedObjectContext *context=[YourAppDelegate managedObjectContext];
 NSManagedObject *note=[NSEntityDescription insertNewObjectForEntityForName:@"Notes" inManagedObjectContext:context];
 [note setValue:notesView.text forKey:@"text"];
 [note valueForKey:@"text"] ;

 NSError *err=nil;
 if (![context save:&err]) {
  NSLog(@"Whoops, couldn't save: %@", [err localizedDescription]);
 }
}

Well if you have a text-entry-entity in your database, just give it an attribute of type date and choose "indexed".

Then I would start an NSFetchRequest like this:

NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Text"
inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];

NSPredicate *predicate = [NSPredicate predicateWithFormat:
                            @"(timeStamp = %@)",datepicker.date];
[request setPredicate:predicate]; //added this line later
NSArray *array = [self.managedObjectContext executeFetchRequest:request error:&error];
//check whether the array has entrys and do something with those objects here

when the user changes the date of the datepicker. The array will be either empty or hold your text-entries. Though maybe you should first read through "Developing with Core Data".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!