Save and Load Data - CoreData

微笑、不失礼 提交于 2020-01-21 07:53:08

问题


I am very 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.

Hope you can help in anyway. Its pretty important I learn how to do this fairly soon, so any help is massively appreciated, thanks.

UPDATE: Thanks for 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.

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

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 thats not a structure or a union:

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

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4177398/save-and-load-data-coredata

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