Using NSManagedObject - iphone

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 data 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.

Related

How can I save an object to a file in Dart and read it again?

I have already done that in Java using ObjectOutputStream and ObjectInputStream classes. Now I want to do this with Dart. I have no idea about what I should use. I have also researched on google but I only found something for serialization of objects and saving them with json. But I am looking for something without json, like in Java.
I'm not english, sorry if I made some mistakes in writing.
Thank you for your time.
I guess you want to store and retrieve your objects to save some important information.
For fastest setup you can use HIVE pub.dev/packages/hive.
Other options are
Serializing your object to son string and saving it to shared preference. https://pub.dev/packages/shared_preferences
Use an orm like moor to store and query large amounts of data. https://pub.dev/packages/moor_flutter

How do I update data in magical records

I am using core data in my swift project and I am using magical record for core data I want to edit already saved data.
I am fetching editing task like this:
array = Tasks.MR_findByAttribute("task_name", withValue: entryLabel.text)
I am getting data which I want to edit. I am not understanding how to edit this and save in the place of old record. Can anyone please tell me the syntax.
In MagicalRecord, you modify data the same way you would with plain Core Data:
fetch
change attributes / relationships
save
The most commonly used API in MagicalRecord for this is the class method saveWithBlock. In the block you fetch your entities and modify them.

Transformable Collection in Core Data with custom Objects

I have to store a collection of custom objects (Dictonary) in Core Data Database.
So far so good. The Dictonary is stored and can be loaded without problems as a "Transformable" object.
The custom Object holds properties, but these are nil after loading them from the Database.
After searching a lot, I haven't found anything for this problem.
It seems that the properties are not getting stored in this way. (Maybe because only the address is stored and not the data??)
Sure it would be better to store an object of Core Data supported datatypes, but in this case the transformable Object is just fine and saves me a lot work and time.
Thank U!
The whole idea of transformable objects is covered in the Core Data Guide. Note that this uses a keyed archiver / unarchiver to create a NSData object from your object or the reverse. This means your customer objects my adhere to NSCoding, and encode all the information in them when asked to as well as handle unencoding.
If your are not doing this now this is the root cause of your problem. What I suggest you do is adopt NSCoding in one custom object, then verify that in fact you can encode it to a NSData object, then from the object unencode it and get the same object back. When you have that working you can then test with Core Data.

Export object from one app to another: XML or Encoding?

I have a fairly complex Core Data database with many entities, attributes and relationships.
I need to take an NSManagedObject subclass object (or its data) and export it to another instance of the app. This other instance needs to then import it into its local database.
I have figured out how to attach files to emails however I am not sure if I should serialise the object to XML or if I should encode it using dictionaries.
It seems like a huge job either way, does anyone have any suggestions?
I dont know much about this but using dictionaries seems a better option to me as you will spare yourself xml writing and parsing, since you are using the same structure why bother with writing xml and parsing.. hope this helps.
update:
see this link.. https://stackoverflow.com/a/1375120/919545

JSON to Persistent Data Store (CoreData, etc.)

All of the data on my application is pulled through an API via JSON. The nature of a good percentage of this data is that it doesn't change very often. So to go and make JSON requests to get a list of data that doesn't change much didn't seem all that appealing.
I'm looking for the most sensible option to have this JSON saved onto the iPhone in some sort of persistent data store. Obviously one plus of persisting the data would be to provide it when the phone can't access the API.
I've looked at a few examples of having JSON and CoreData interact, for example, but it seems that they only describe transforming NSManagedObjects into JSON. If I can transform JSON into CoreData, my only problem would be being able to change that data when the data from the API does change.
(Or, maybe this is all just silly.)
For some conceptual reading, you may want to read documentation about Efficiently Importing Data, especially "Find-or-create". See a previous similar question.
Getting JSON and saving it as Core Data locally is highly reasonable. How I do it is in two stages:
convert JSON to native Cocoa data types (NSDictionary and NSArray)
convert NS* into Core Data object
Two good frameworks for converting between JSON and NS* are json-framework and TouchJSON. The below example is based on json-framework.
Let's say you get an array of some objects from JSON. You'd then do:
NSArray *objects = [jsonStringYouGotFromServer JSONValue];
for (NSDictionary *_object in objects) {
CDObjectType *cdObject = [self cdObjectFromDictionary:_object];
// cdObject is now a full-featured Core Data object
}
the cdObjectFromDictionary might be something like this:
- (CDObjectType *) cdObjectFromDictionary:(NSDictionary *)dict {
CDObjectType *object = [NSEntityDescription
insertNewObjectForEntityForName:#"Object"
inManagedObjectContext:moc];
NSDictionary *attributes = [[NSEntityDescription
entityForName:#"Object"
inManagedObjectContext:moc] attributesByName];
for (NSString *attr in attributes) {
[object setValue:[dict valueForKey:attr] forKey:attr];
}
return object;
}
The above assumes that the attribute names in your JSON and Core Data model are the same and that the data types match (i.e JSON numbers are Core Data NSNumbers etc). The above code works nicely even if the model changes.
The above code does not consider how to test if the object already exists locally, but you can imagine how to add that. You must have an object ID of some form in your model, and you can see if the object exists locally before adding it, or whether existing object needs to be updated.
Accessors may be needed when there are to-many relations.
It is more efficient to use accessors than to use key-value. (Internally Core Data may use key-value anyway, so this point may not be true if that is the case.)
Using subclass and properties, the compiler can help you to detect problems, while using key-value, the problems will only show at runtime and harder to fix.
Therefore, it is also good to subclass NSManagedObject and use generated properties and accessors.