iOS CoreData - Serialize entire object instead of creating Entity - ios

I am working on an email client and I want to persist emails into CoreData.
Looking at some tutorials, it seems I need to create a custom class (entity) and add attributes for each field I want to persist.
The problem is that I have an array of pretty complex objects (http://libmailcore.com/mailcore2/api/Classes/MCOIMAPMessage.html) I want to serialize and persist. Is there an easier way to do this?

Try converting the MCOIMAPMessage to NSData and then you can persist it to coredata. Check this SO post on how to convert NSObject to NSData. Hope this helps.

Related

Can you save Parse information in NSUserDefaults? (IOS)

Basically, i want to save my current Parse user's friends into NSUserDefaults. For some reason it doesn't seem to be saving correctly or it won't let me save them. Is this because the array of objects(friends) that i am returning is to intricate for NSUserDefaults to handle?
I have tried turning the array of objects into NSData and saving it that way but i still haven't yielded any good results.
I would really appreciate if someone could help me out.
Cheers.
You really should not use NSUserDefaults for storing this type of information. NSUserDefaults was created in order to store and retrieve "settings" type information as opposed to data.
As for your question, in order to save custom objects you need to implement these 2 methods:
- (void)encodeWithCoder:(NSCoder *)encoder {
}
- (id)initWithCoder:(NSCoder *)decoder {
}
These methods will essentially tell the application how to store the objects data in a file as well as how to read it back in and create an object from it.
Documentation:
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Archiving/Articles/codingobjects.html
However, if you are looking to store these objects locally you should look into Parse's local caching feature. You could also use Core Data to store the objects locally but maintaining 2 instances of the objects may be ugly.
Parse Local Caching:
https://www.parse.com/docs/ios_guide#localdatastore/iOS

How to save NSMutableArray to Core Data?

I know it's not recommended to save NSMutableArray to Core Data but I need to access the array on another view controller (using one of the segue methods (such as passing a variable to another view controller) won't do the job).
So unless there's another way that I'm not aware of, how can I save the array to core data?
I'm using Swift.
I think your question is not how to save NSMutableArray to Core Data, instead, your question seems like is How to access data on another viewController?
First, I think you can get some knowledge about Delegate.
Second,Here is a Q&A I can share with you which I think may help you to figure out your confused.Passing Data between View Controllers
Saving NSArray to Core Data is possible via Transformable-type attributes.
Saving NSMutableArray is not possible. Only immutable ones.
if you want to save a mutable array to core data, use copy method. It will return immutable copy of your mutable object.

IOS: store a json with array and object nested

I have a question about creating a database with core data.
In my app at first start I should parse some json to obtain some data to insert in core data db.
my json files are structured in this way: (I show only an element of my json)
[{"id":"s1",
"n":"Name hotel",
"id_loc":["l1","l2","l3","l4"],
"val":3,
"tel1":"12345678",
"tel2":"12345678",
"obj":
{"id":"o1",
"n":"Name",
"des":"description",
"flag":"red"}
}]
I understand that I can consider this as an entity in coredata and consider all element as attribute, it's clear.
Now you can see that inside my json there is an array "id_loc" and an object (or dictionary) "obj".
In core data what's the way to manage these two elements?
I suppose that "obj" can be managed as a new entity, and "id_loc", what's the way to set it in my core data DB?
Can you help me?
Thanks
For obj, it's as you suggest: create a new entity, and set up a relationship between the two entities.
For id_loc it depends on how you need to use the data.
If you just want to have that data available when you look up an instance (that is, you maybe display this data but don't ned to search for it), you can store the strings in an NSArray. Make the attribute transformable in the Core Data model editor, and Core Data will read/write the complete array.
If you need to look up data based on id_loc values (for example: Find every object where id_loc contains l3), the best approach is to create another entity to hold values of id_loc, and set up a to-many relationship to that new entity.

iOS - Saving NSMutableArray to iPhone

I'll ask in the form of a hypothetical, which might make it easier for me to explain.
I have a class called Person, and in this has three fields:
NSString *name;
NSDate *dateOfBirth; and
NSMutableArray *friends.
An example object is this:
name = "John Smith"
dateOfBirth = 01/04/1985
friends = "Simon Scott"; "Jennifer Lane"; "Mary Firth"
Once the user has filled the NSMutableArray with the data they want, what would be the best way to save this data to the iPhone? I would anticipate that there could be up to 100 instances of the Person object, and all that will be required is the displaying of this data in a UITableView and giving the user the ability to add and remove entries at their will.
I have seen multiple suggestions on this site, which include NSDictionaries and using the writeToFile method, but before I research one of these, I was hoping someone could point me in the right direction? I would like to ensure that I'll be using the easiest and most appropriate method that's out there.
Many thanks.
Please take a look at the Property List Programming Guide. As long as you stick with a core set of object types for your data, you can write and read your data from a file or URL in one line, like this:
[people writeToURL:someURL atomically:NO];
The types you've mentioned in your question (strings, dates, arrays, dictionaries) can all be written to a property list.
#Achiral,
It really depends on what you want to do with the data and how concerned you are with the flexibility of your code.
However, my recommendation would be to use CoreData to make an SQLlite database and make a 'Person' entity with the properties that you list above. I don't know if you are familiar with CoreData, but it is highly optimized on iOS and is pretty easy to use, since it has a pretty simple 'fill in the blanks' style form for creating the data models. You should also note that CoreData is a well accepted and supported way to store data in an iOS and OS X app.
I hope this helps.

NSKeyedUnarchiver with custom Objects

I have a question about NSKeyedUnarchiver, NSMutableArray and self-created classes. The data is stored by NSKeyedArchiver to user defaults. When the data load into the user defaults will be printed to the console of the data stream. I want to unpack the data into an array, the array will be empty.
Maybe someone of you knows how I save the array or the array load.
Thank you.
Greetings,
Schumi
You should go through this documentation first. But basically, To enable archiving on custom class objects you will have to adopt the NSCoding protocol and implement initWithCoder: and encodeWithCoder: methods.

Resources