Hardcoding value into Coredata using RestKit for iOS - ios

I am using the latest RestKit for iOS. I have a few different APIs that I want to map into the same CoreData Object. I am trying to hard code a value in field on the object that identifies which API the data came from.
Does anyone know if this is possible using the RKEntityMapping for RestKit?

You need to set your API field, which will store this value, to transient in your core data object. Next add block with setWillMapDeserializedResponseBlock: in which you add an appropriative indicator value to the mapping dictionary.

Related

Saving an array of objects in Core Data swift 3

So I'm trying to take an array of objects that already exist and then persist the data using core data. I'm slightly confused if I need to map out all of the properties in that custom object, or if I can just save every object directly?
An example would be something like making an entity called CoreDataArrayObj, and then making the custom object an attribute? Or would I need to take that CoreDataArrayObj entity and make all of the properties within the custom object such as string1Prop: String, string2Prop: String, Int1Prop: Int etc...?

CoreData Unique: Decide update or create

I'm using CoreData in my project and I'm thinking about unique fields and creating objects or updating them if they are already existing.
UseCase:
Get JSON from Server
Map JSON to Object
Save to CoreData
What I want to do is:
Get JSON from Server
Map JSON to Object
Does the object already exists (Unique field is unique identifier for object)
If YES
Get object
Update fields
If NO
Create object
Save to CoreData
Isn't that a lot overhead for the solution? So every time I get an object I have to check the CoreData. Is there something that can do this handeled by CoreData internally?
Have a look a MagicalRecord it has built in maps from JSON to core data and will keep unique items unique.

RestKit : How to parse the JSON response to NSDictionary without mapping any entity or class?

I just want the RestKit to parse the JSON data into NSDictionary, but not a class. This is because the attributes of the JSON data is dynamic, means the number of fields is not fixed and field count can be large. So I don't want to create a huge class to map the json data. Just keep that in NSDictionary. Does RestKit provide this functionality or we have to work out some other way.Guidance Needed.
Thanks.
Have to modify the Restkit to support the ability ....
RKObjectRequestOperation.HTTPRequestOperation.responseData
then parse the json data to dictionary or array
Either use AFNetworking (which RestKit is built on top of) or use a dynamic mapping (depending on your destination class needs).
If you have large arbitrary data then you should just avoid using RestKit as it will only slow your performance.

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.

RestKit 0.20 and ManagedObjectContexts

I am mapping data using RestKit 0.20 into a Core Data and displaying it in a UITable. I am writing the data, an 'Activity' object, to the mainQueue's ManagedObjectContext and it all works fine. Now I need a second table with Future-Activities and also a third table with Past-Activities. I need a ManagedObjectContext for each table as the sorting is done on the server side. How can I handle this and have persistent data. Is 'newChildManagedObjectContextWithConcurrencyType' what I need to use?
Keep a single store. Use a predicate to filter out the items you want.
If you can download all of the data (and you're happy to do that even though some of it may not be used by the user), and you can tag them for what they are used for then that is an option.
From a RestKit point of view, you can use metadata to tag the items during the mapping process so that you know how they should be used (and then filter on that). This requires that you add a new key to the item - but, if one item could be in all responses this will be problematic because the values would get overwritten.
To use metadata, simply add a new mapping like:
#"#metadata.URL": #"requestURL"
Where #metadata.URL is the URL used to make the request and requestURL is the property on your entity that you can use for filtering. The predicate will check for contains your types ("all_day" "start_time" "end_time").

Resources