开发者

Using NSManagedObject

I'm learning Core Data and trying to parse XML from some web service and save it into data storage.

Is it the best practice to save da开发者_JAVA技巧ta from XML directly into managed objects? Or there are some better ways to do this operation?

Thank you!


You should be convert your data into something that is useful for the application and not just store the raw tagged data when parsing the XML. That way you only do the conversion once in the XML Parser and not every time you want to use the value elsewhere in the code.

For example, an integer stored in an XML file is always going to appear as a string at first, so using [NSNumber numberWithInt:[string intValue]] in the XML parser once is far better than having that extra bit of code peppered throughout your application.


I think you didnt understand what i mean. If i want to parse some musical album info from XML and save it into Data Storage should make something like Album* album = [NSEntityDescription insertNewObjectForEntityForName:@"Album" inManagedObjectContext:context]; to create object where i will save data from XML or i have to create some another Album class not inherited from NSManagedObject to work with it while parsing


I don't think there's a direct way do convert an XML document into a managed object. I use NSXMLDocument to do exactly what you want to do but you will have to do a little parsing.


I would create your objects directly from your parsed XML. In other words, just use your NSManagedObject derived classes. No intermediate class hierarchy is necessary. Also, if you're not already, you should use mogenerator + xmod for re-generating your data object classes automatically whenever your model changes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜