开发者

Save and Load Data - CoreData

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

Update

Thanks fo开发者_如何学Gor the replies, especially the for the code sample. However I have some issues understand how this fits together, forgive me. I want to learn how this works, but to get it working in the mean time will really be helpful.

Presumably I put your first code chunk in the did finish editing method (save when leaving text box) and then the second chunk in the date changed method (load when changing the date)?

This code, does this use the entity and property names? How exactly do I need to setup my xcdatamodel file? Do I need to use this code or does this just demonstrate what entities and properties I need?

DatedText{
    savedText:String
    dateSaved:Date
}

This line says 'mo' is undeclared: (FIXED)

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

Presumably below 'someText' is the text I want save? And the 'aDateObject' is the date in my UIDatePicker?

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

Request for member 'moc' 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];

Request for member 'moc' in something that's not a structure or a union:

NSArray *fetchedObjs=[self.moc executeFetchRequest:theFetch error:&fetchError];


To start you need a data model to reflect 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 custom class) do:

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 retrieve a DatedText object with a specific date you create a fetch like so:

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.


There are lots of resources about Core Data, you could follow the following links to learn it. http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/iPhoneCoreData01/Introduction/Introduction.html Core Data Tutorial for iOS: this is really great, official document, easy to learn, with downloadable source. Any good guide about iPhone Core Data? Someone asked the same question before, you can take a look.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜