I have a JSON payload that is being returned from my server and is structured like this:
{"users":[users]}
Using a simple RKObjectMapping and RKResponseDescriptor, I'm able to successfully map this JSON into a collection of User objects when I use getObjectsAtPath, but now I'd like to make a slight modification to the JSON server-side. Namely, I'd like to add a key-path
{"users":[users], "more":true}
where the key-path "more" indicates whether there are more users to load that are not included in the "users" array. The problem I'm having is that I can't find a simple way to access the value of this "more" key-path. Ideally, I'd like to define a Mapping and Response Descriptor that map "more" into a BOOL (or NSNumber), but I'm having trouble figuring out how to do this. Adding a mapping like
RKObjectMapping *moreMapping = [RKObjectMapping mappingForClass:[NSNumber class]]
with ResponseDescriptor
[RKResponseDescriptor responseDescriptorWithMapping:moreMapping pathPattern:pathPattern
keyPath:#"more" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
Doesn't do the trick. Ultimately, in the success block of getObjects, I'd like my RKMappingResult to be structured like so:
#{#"users":[users], #"more":1}
Any tips?
RestKit is based around mapping data into objects, not to objects in the way you're after. The closest you can get is to map the true into an NSNumber inside a dictionary or array. Try:
RKObjectMapping *moreMapping = [RKObjectMapping requestMapping];
[moreMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:#"more"]];
Related
I have used RKObjectMapping for parsing my JSON response using RestKit.
For ex:.
RKObjectMapping *menuItemMapping = [RKObjectMapping mappingForClass:[MenuCategoryItem_arr class]];
[menuItemMapping addAttributeMappingsFromArray:#[#"item_id",
#"name",
#"item_description",
#"price",
#"offer_price"]];
I have used [RKObjectManager postObject:path:parameters:success:failure:].
This all works fine there's no issue with these mechanisms.
My question is what if i have large number of properties in a class? lets say 50 or 60? how would you map these number of properties in a shorter way?
I have an Rest API which may have more than 40 properties? do i have to define all attribute using addAttributeMappingsFromArray:? or is there any other way?
Thanks in advance
I'm a nwebie in Core Data, i have designed a navigation based application and some of the data i use are created on run time(come from a URL via JSON). I took a few tutorials an searched for almost a day but haven't still realized how to save the incoming JSON data to the Entity (or event?) in my Core Data model. I fetch the data in the DetailViewController class and i need to save this data to Core Data(I have prepared an Entity with 7 properties). Can anyone please help?(If you know a good tutorial or sample code i will be pleased)
EDIT This may be a little specific but i really have trouble with and need just a little help.
My data comes to the app from a kind of restful server(i wrote it in PHP), firstly user enters his/her login informations(which i have saved to the database on server before) and when the response data comes i will use different elements of it in differen views(for example the user_id will be used on a view and the buttonData etc on other views). My question is, how will i save JSON data into my core data model(has tree Entities for the moment). Thanks in advance
Note: I lokked arround a lot but couldn't find any answer&tutorial about an app like mine
The best way to do that would be to create entities corresponding to JSON structure. Easiest was is when each JSON object becomes an entity, and arrays become arrays of entities. Be reasonable, however, and don't introduce too much overkill for JSON subobjects that are essentially part of its superobject.
When you have created entities, you can start off with the parsing and translation. Use some JSON framework (starting from iOS5 there's one from Apple) and parse JSON string into object tree, where root item is either an NSArray or NSDictionary, and subelements will be NSArray, NSDictionary, NSNumber, NSString or NSNull.
Go over them one by one in iterational loops and assign according values to your core data entity attributes. You can make use of NSKeyValueCoding here and avoid too much manual mapping of the attribute names. If your JSON attributes are of the same name as entity attributes, you'll be able to just go over all dictionary elements and parse them into attributes of the same name.
Example
My parsing code in the similar situation was as follows:
NSDictionary *parsedFeed = /* your way to get a dictionary */;
for (NSString *key in parsedFeed) {
id value = [parsedFeed objectForKey:key];
// Don't assign NSNull, it will break assignments to NSString, etc.
if (value && [value isKindOfClass:[NSNull class]])
value = nil;
#try {
[yourCreatedEntity setValue:value forKey:property];
} #catch (NSException *exception) {
// Exception means such attribute is not defined in the class or some other error.
}
}
This code will work in trivial situation, however, it may need to be expanded, depending on your needs:
With some kinds of custom mappings in case you want your JSON value be placed in differently named attribute.
If your JSON has sub-objects or arrays of sub-objects, you will need to detect those cases, for example in setters, and initiate new parsing one level deeper. Otherwise with my example you will face the situation that assigns NSDictionary object to an NSManagedObject.
I don't think it is reasonable to dive into these, more advanced matters in scope of this answer, as it will expand it too much.
I suggest you to use this library : https://github.com/TouchCode/TouchJSON
And then if you want to make a factory to parse json and feed your code data, you can use selectors to call methods to fill all your attributes.
Chances are your JSON data gets converted to an NSDictionary or NSArray (or some combination of the two). Simply extract the key/values from the JSON structure and add them to your entity class.
This lib helps me lot
Features
Attribute and relationship mapping to JSON key paths.
Value transformation using named NSValueTransformer objects.
Object graph preservation.
Support for entity inheritance
Works vice-versa
i would like to have some clarification about the correct approach to prefill a (complex) DB with multiple entities.
This is the approach i want to use:
Creation of a separate project that cares about DB Population
In case of Entities with no relations: parsing of a .json file in order to gather information of the Entity
In case of Entities with relation with other Entities (1:N relation):
I have a .json file for each entity
I allocate a Managed Object for each element involved with the relation between the Entities and i fill their attributes.
Please consider the example below for better explaination:
Let's consider the DB contains two Entities: Owner and House with a (1:N) relation.
In order to prefill the DB I:
configurea owner.json file and a house.json file
parse owner.json and gather and create a ManagedOwnerObject
parse house.json and create a set of ManagedHouseObject that have a relation with the first object created.
fill the attribute of ManagedOwnerObject that describe the relation with ManagedHouseObject with the NSSet just created
Iterate the process for all the house.json
Now, this process seems to me a little complex considering that my application has something like 10 Entities connected with 1:N relation.
Could you please suggest me if i'm doing right or if other better solutions could be considered?
Kind regards
Nicolò
You could simply fill the database manually using your app and ship the resulting SQLite file with your app instead of creating an empty one when the user opens the app for the first time.
EDIT:
Using KVC could simplify the creation of the objects. Setting the relationships probably isn't that easy. I would go through all of the data twice. Create the object without relationships in the first run and set up the relationships in the second.
Here is a method to do something similar, without relationships though:
-(void)createEntities:(NSString *)entityName fromFile:(NSString *)filePath inManagedObjectContext:(NSManagedObjectContext *)managedObjectContext {
// Remove all existing objects of this entity.
[self removeExistingEntriesForEntityName:entityName managedObjectContext:managedObjectContext];
// Read all data from file path.
NSArray *newObjects = [self readTestDataFromFilePath:filePath];
// Insert new object for all existing keys.
for (NSDictionary *no in newObjects) {
NSManagedObject *managedObject = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:managedObjectContext];
for (NSString *key in no.keyEnumerator) {
[managedObject setValue:[no valueForKey:key] forKeyPath:key];
}
}
[(AppDelegate *)[[UIApplication sharedApplication] delegate] saveContext];
}
I'm posting objects on a server as JSON that consists of a few attributes and an ID that is the primary key attribute. I'm also using Core Data to save all the objects locally.
The problem is that when I first create the object to POST I know all the attributes but the unique ID. The ID is set at server-side, and when I get the response from the server I have ended up with two objects in my database:
One with ID 0, and one with the real ID.
Is there any way to get restkit/coredata to treat these two objects as the same, or alternatively don't save the first object in the database?
You can use the postObject: usingBlock method and assign a target object for the object loader like this.
[[RKObjectManager sharedManager] postObject:myObject usingBlock:^(RKObjectLoader *loader) {
loader.targetObject = myObject;
loader.delegate = self;
loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[myObject class]];
}
Note that you also have to set the delegate manually in the block of code every time you run the method.
I'm a nwebie in Core Data, i have designed a navigation based application and some of the data i use are created on run time(come from a URL via JSON). I took a few tutorials an searched for almost a day but haven't still realized how to save the incoming JSON data to the Entity (or event?) in my Core Data model. I fetch the data in the DetailViewController class and i need to save this data to Core Data(I have prepared an Entity with 7 properties). Can anyone please help?(If you know a good tutorial or sample code i will be pleased)
EDIT This may be a little specific but i really have trouble with and need just a little help.
My data comes to the app from a kind of restful server(i wrote it in PHP), firstly user enters his/her login informations(which i have saved to the database on server before) and when the response data comes i will use different elements of it in differen views(for example the user_id will be used on a view and the buttonData etc on other views). My question is, how will i save JSON data into my core data model(has tree Entities for the moment). Thanks in advance
Note: I lokked arround a lot but couldn't find any answer&tutorial about an app like mine
The best way to do that would be to create entities corresponding to JSON structure. Easiest was is when each JSON object becomes an entity, and arrays become arrays of entities. Be reasonable, however, and don't introduce too much overkill for JSON subobjects that are essentially part of its superobject.
When you have created entities, you can start off with the parsing and translation. Use some JSON framework (starting from iOS5 there's one from Apple) and parse JSON string into object tree, where root item is either an NSArray or NSDictionary, and subelements will be NSArray, NSDictionary, NSNumber, NSString or NSNull.
Go over them one by one in iterational loops and assign according values to your core data entity attributes. You can make use of NSKeyValueCoding here and avoid too much manual mapping of the attribute names. If your JSON attributes are of the same name as entity attributes, you'll be able to just go over all dictionary elements and parse them into attributes of the same name.
Example
My parsing code in the similar situation was as follows:
NSDictionary *parsedFeed = /* your way to get a dictionary */;
for (NSString *key in parsedFeed) {
id value = [parsedFeed objectForKey:key];
// Don't assign NSNull, it will break assignments to NSString, etc.
if (value && [value isKindOfClass:[NSNull class]])
value = nil;
#try {
[yourCreatedEntity setValue:value forKey:property];
} #catch (NSException *exception) {
// Exception means such attribute is not defined in the class or some other error.
}
}
This code will work in trivial situation, however, it may need to be expanded, depending on your needs:
With some kinds of custom mappings in case you want your JSON value be placed in differently named attribute.
If your JSON has sub-objects or arrays of sub-objects, you will need to detect those cases, for example in setters, and initiate new parsing one level deeper. Otherwise with my example you will face the situation that assigns NSDictionary object to an NSManagedObject.
I don't think it is reasonable to dive into these, more advanced matters in scope of this answer, as it will expand it too much.
I suggest you to use this library : https://github.com/TouchCode/TouchJSON
And then if you want to make a factory to parse json and feed your code data, you can use selectors to call methods to fill all your attributes.
Chances are your JSON data gets converted to an NSDictionary or NSArray (or some combination of the two). Simply extract the key/values from the JSON structure and add them to your entity class.
This lib helps me lot
Features
Attribute and relationship mapping to JSON key paths.
Value transformation using named NSValueTransformer objects.
Object graph preservation.
Support for entity inheritance
Works vice-versa