Saving and loading data - CoreData

I am very new to CoreData and to my iPhone app. I want to know how to save some text and then load it back. But the trick is to load it back when the date in UIDatePicker is the same as when I saved it as a calendar.

I hope you can help. Its quite important, I will learn how to do it pretty soon, so any help received a lot of thanks, thanks.

UPDATE: Thanks for the answers, especially for the sample code. However, I have some problems that understand how this fits together, forgive me. I want to know how this works, but getting it to work on average will be really helpful.

I understand some of these errors, only correcting them in the correct manner is my problem. Hope you can help.

Presumably, I put your first code snippet in the finished editing method (saving when you exit the text field), and then the second piece in the method with the modified date (loading when the date changes)?

Does this code use object and property names? How exactly do I need to configure the xcdatamodel file? Do I need to use this code, or does it just show which objects and properties I need?

DatedText{ savedText:String dateSaved:Date } 

This line says that "mo" is not declared: (FIXED)

  newDatedText=mo=[NSEntityDescription insertNewObjectForEntityForName:@"DateText" inManagedObjectContext:theManagedObjectContext]; 

Allegedly below is "someText" is the text I want to save? Is 'aDateObject' the date of my UIDatePicker?

 [newDatedText setValue:someText forKey:@"savedText"]; [newDatedText setValue:aDateObject forKey:@"dateSaved"]; 

The query for the 'moc' member in something not a structure or union:

 NSEntityDescription *testEntity=[NSEntityDescription entityForName:@"DatedText" inManagedObjectContext:self.moc]; 

Whats 'targetDate' (undeclared)? UIDatePicker date again?

 NSPredicate *pred=[NSPredicate predicateWithFormat:@"dateSaved==%@", targetDate]; 

The request of the 'moc' member in what is not a structure or union:

 NSArray *fetchedObjs=[self.moc executeFetchRequest:theFetch error:&fetchError]; 
+4
source share
2 answers

First you need a data model that reflects what you want to save, in this case, some text and a date. So:

 DatedText{ savedText:String dateSaved:Date } 

To create a new DatedText object (without a special class), follow these steps:

 NSManagedObject *newDatedText; newDatedText=[NSEntityDescription insertNewObjectForEntityForName:@"DateText" inManagedObjectContext:theManagedObjectContext]; [newDatedText setValue:someText forKey:@"savedText"]; [newDatedText setValue:aDateObject forKey:@"dateSaved"]; NSError *saveError=nil; [theManagedObjectContext save:&saveError]; if (saveError!=nil) { NSLog(@"[%@ saveContext] Error saving context: Error=%@,details=%@",[self class], saveError,saveError.userInfo); } 

To get a DatedText object with a specific date, you create a selection as follows:

 NSFetchRequest *fetch=[[NSFetchRequest alloc] init]; NSEntityDescription *testEntity=[NSEntityDescription entityForName:@"DatedText" inManagedObjectContext:self.moc]; [fetch setEntity:testEntity]; NSPredicate *pred=[NSPredicate predicateWithFormat:@"dateSaved==%@", targetDate]; [fetch setPredicate:[NSArray arrayWithObject:pred]]; NSError *fetchError=nil; NSArray *fetchedObjs=[theManagedObjectContext executeFetchRequest:theFetch error:&fetchError]; if (fetchError!=nil) { NSLog(@" fetchError=%@,details=%@",fetchError,fetchError.userInfo); return nil; } 

fetchedObjs will now contain an array of all DatedText objects with the same savedDate as targetDate .

+3
source

There are many resources in Core Data to learn it. http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/iPhoneCoreData01/Introduction/Introduction.html Basic Data Tutorial for iOS: This is a really cool, official document, easy to learn, with a downloadable source code. Any good guide on Core Core data? Someone asked the same question before, you can take a look.

+2
source

All Articles