Restkit NSInternalInconsistencyException Unable to add mapping for keyPath - ios

Fyi, I'm not too familiar with Restkit's API. I prefer other means of deserializing json into domain objects; however, I've inherited a project and this is the quickest solution to fix a bug.
I'm trying to map "assignments" in the below json to "doctorAssignments" which is a 1-Many relationship in Core Data.
example json:
{
"id": 43,
"time_zone": "Mountain Time (US & Canada)",
"accepting_patients": true,
"auto_set_context_id": 44,
"audio_notifications": false,
"assignments": [
{
"id": 14,
"active_since": "2015-06-17T23:26:03Z",
"doctor": {
"id": 44,
"name": "Darth Vader PsyD",
"title": "Dr.",
"bio": "",
"type": "Doctor",
"profile_image_url": "https://cirrusmd-staging.s3.amazonaws.com/uploads/doctor/profile_image/43/darth.png?X-Amz-Expires=600&X-Amz-Date=20150618T163351Z&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJGWZJVKLZCGMXJ2Q/20150618/us-east-1/s3/aws4_request&X-Amz-SignedHeaders=host&X-Amz-Signature=b0bc0163f4cf8c3c73572198c596565b0012a33331936baf4b14b9a255c0870f",
"credential_id": 214
}
}
],
"practice": {
"id": 2,
"name": "Test Practice",
"address": "",
"phone": "",
"fax": "",
"created_at": "2013-06-11T17:49:48Z",
"updated_at": "2013-06-11T17:49:48Z",
"email": "",
"city": "",
"state": "",
"zip": ""
}
}
Mapping:
+ (RKEntityMapping *)responseMapping {
if (_responseMapping == nil) {
_responseMapping = [RKEntityMapping mappingForEntityForName:[CMDUser entityName] inManagedObjectStore:[NSManagedObject managedObjectStore]];
_responseMapping.identificationAttributes = #[ CMDUserAttributes.userID, CMDUserAttributes.type ];
[_responseMapping addAttributeMappingsFromDictionary:#{
#"id" : CMDUserAttributes.userID,
#"type" : CMDUserAttributes.type,
#"profile_image_url" : CMDUserAttributes.profilePic,
#"credential_id" : CMDUserAttributes.credentialID,
#"name" : CMDUserAttributes.name,
#"last_name" : CMDUserAttributes.lastName,
#"first_name" : CMDUserAttributes.firstName,
#"allergies" : CMDUserAttributes.allergies,
#"medications" : CMDUserAttributes.medications,
#"history" : CMDUserAttributes.history,
#"email" : CMDUserAttributes.email,
#"location" : CMDUserAttributes.location,
#"zipcode" : CMDUserAttributes.zipcode,
#"gender" : CMDUserAttributes.gender,
#"dob" : CMDUserAttributes.dob,
#"relationship.state" : CMDUserAttributes.relationshipState,
#"relationship.status" : CMDUserAttributes.relationshipStatus,
#"relationship.onboarded_at" : CMDUserAttributes.onboardedAt,
#"auto_set_context_id" : CMDUserAttributes.autoSetContextID,
#"phone" : #"phone",
}];
}
// taking this line out removes the exception, but then the json isn't mapped
[_responseMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"assignments" toKeyPath:#"doctorAssignments" withMapping:[CMDDoctorAssignment mapping]]];
return _responseMapping;
}
On launch I get this error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to add mapping for keyPath doctorAssignments, one already exists...'
Note: I've omitted some fields in the json. In the mapping, I do NOT have
#"assignments": #"doctorAssignments".

Related

How to order by two different attributes on same document in elasticsearch?

I have documents as
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "journeys-development-latest",
"_type" : "_doc",
"_id" : "1399",
"_score" : 1.0,
"_source" : {
"draft_recent_edit_at" : "2023-01-14T04:16:41.318Z",
"recent_edit_at" : "2022-09-23T14:13:41.246Z"
}
},
{
"_index" : "journeys-development-latest",
"_type" : "_doc",
"_id" : "1394",
"_score" : 1.0,
"_source" : {
"draft_recent_edit_at" : "2022-07-02T16:19:41.347Z",
"recent_edit_at" : "2022-12-26T10:12:41.333Z"
}
},
{
"_index" : "journeys-development-latest",
"_type" : "_doc",
"_id" : "1392",
"_score" : 1.0,
"_source" : {
"draft_recent_edit_at" : "2022-05-20T11:33:41.372Z",
"recent_edit_at" : "2021-12-21T03:36:41.359Z"
}
}
]
}
}
What I know is if I do
{
"size": 12,
"from": 0,
"query": {
......,
......
},
"sort": [
{
"recent_edit_at": {
"order": "desc"
}
}
]
}
This will order by recent_edit_at in desc order.
Similarly replacing recent_edit_at with draft_recent_edit_at will order by draft_recent_edit_at in desc order.
What I am struggling is to find a way where I can say I want to order by max in draft_recent_edit_at, recent_edit_at and then order the documents according to those.
===========================Update===========================
After adding sort proposed by HPringles the output is
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"Math.max(doc['draft_recent_edit_at'].value.toInstant().toEpochMilli(),\n doc['recent_edit_at'].value.toInstance().toEpochMilli())\n ",
" ^---- HERE"
],
"script": "\n Math.max(doc['draft_recent_edit_at'].value.toInstant().toEpochMilli(),\n doc['recent_edit_at'].value.toInstance().toEpochMilli())\n ",
"lang": "painless"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "journeys-development-latest",
"node": "GGAHq1ufQQmSqeLRyzka5A",
"reason": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"Math.max(doc['draft_recent_edit_at'].value.toInstant().toEpochMilli(),\n doc['recent_edit_at'].value.toInstance().toEpochMilli())\n ",
" ^---- HERE"
],
"script": "\n Math.max(doc['draft_recent_edit_at'].value.toInstant().toEpochMilli(),\n doc['recent_edit_at'].value.toInstance().toEpochMilli())\n ",
"lang": "painless",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "dynamic method [org.elasticsearch.script.JodaCompatibleZonedDateTime, toInstance/0] not found"
}
}
}
]
},
"status": 400
}
If I'm understanding correctly, you can do this with a painless script at runtime.
See below:
"sort": {
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": """
Math.max(doc['draft_recent_edit_at'].value.toInstant().toEpochMilli(),
doc['recent_edit_at'].value.toInstance().toEpochMilli())
""",
"params": {
"factor": 1.1
}
},
"order": "asc"
}
}
This will work out the maximum of the two, and then sort based on that value.
As far as I know you might also want to convert the Epoch values to long.
Something like -
"sort": {
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": """
long draft_recent_edit_at = doc['draft_recent_edit_at'].value.toInstant().toEpochMilli();
long recent_edit_at = doc['recent_edit_at'].value.toInstant().toEpochMilli();
Math.max(draft_recent_edit_at, recent_edit_at);
"""
},
"order": "asc"
}
}

Data conversion from JSON results as NSDictionary[] throws EXC_BAD_INSTRUCTION runtime error

I have the following JSON being received in my Swift code, after being parsed as a NSDictionary. In my function I am trying to extract the JSON objects in "results" block as NSDIctionary[], but this is throwing a run-time error. I don't understand why, as this was working just a few days earlier.
{
"results": [
{
"id": "3",
"name": "The National",
"slug": "thenational",
"facebook_url": "https://www.facebook.com/thenationalofficial/",
"twitter_url": "https://twitter.com/The_National",
"profile_image": "http://example.staging.com/media/profile_image/thumbnail_263x263/1352756032.jpg",
"_type": "artist",
"resource_uris": {
}
},
{
"id": "5",
"name": "Mayer Hawthorne",
"slug": "mayerhawthorne",
"facebook_url": "https://www.facebook.com/MayerHawthorne",
"twitter_url": "https://twitter.com/MayerHawthorne",
"profile_image": "http://example.example.com/media/profile_image/thumbnail_263x263/1352755133.png",
"_type": "artist",
"resource_uris": {
}
},
{
"id": "20",
"name": "I Play Maracas",
"slug": "iplaymaracas",
"facebook_url": "",
"twitter_url": "",
"profile_image": "http://staging.wedemand.com/images/en/img-list-home.gif",
"_type": "artist",
"resource_uris": {
"_demanded_by": null,
"demand_url": "http://ec2-54-86-17-163.compute-1.amazonaws.com/artists/20/?demand=1&access_token={}",
"dismiss_url": "http://ec2-54-86-17-163.compute-1.amazonaws.com/artists/20/?demand=0&access_token={}"
}
},
{
"id": "35",
"name": "Black SuperHeros",
"slug": "blacksuperheros",
"facebook_url": "",
"twitter_url": "",
"profile_image": "http://staging.example.com/images/en/img-list-home.gif",
"_type": "artist",
"resource_uris": {
}
},
{
"id": "49",
"name": "Ayman Elgadi",
"slug": "aymanelgadi",
"facebook_url": "",
"twitter_url": "",
"profile_image": "http://staging.example.com/images/en/img-list-home.gif",
"_type": "artist",
"resource_uris": {
}
},
{
"id": "8874",
"name": "Lauri",
"slug": "lauri",
"facebook_url": "http://www.facebook.com/hughlaurieblues",
"twitter_url": "http://twitter.com/hughlaurieblues",
"profile_image": "http://staging.example.com/media/profile_image/thumbnail_263x263/lauri_profilepic.jpg",
"_type": "artist",
"resource_uris": {
}
}
]
}
My IOS-Swift code receives the NSDictionary object after being parsed by AFNetworking lib and passes to the function which casts the results array as NSDictionary[], is now throwing run-time error, while earlier this was working.
(operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in println("JSON: " + responseObject.description)
var jsonResult: NSDictionary = responseObject as NSDictionary
this jsonResult is passed to the function below which tries to cast as NSDictionary[]
let allResults: NSDictionary[] = results["results"] as NSDictionary[]
UPDATE:
I printed the class of the results object as it is being returned as __NSCFDictionary.
Here What is an NSCFDictionary? is a discussion regarding this and says to use this just like NSDictionary, but in my case its not working.
The value of results is not a dictionary in your JSON, it is an array. You should get it with something like this;
let allResults: NSArray = results["results"] as NSArray

Parsing SoundCloud - Change API Call, not receiving JSON data?

I'm playing around with the SoundCloud iOS tutorial and have been changing the API calls to get different information.
However, I can't seem to get any JSON back for a call I make to the API when I try to do a GET request on the URL https://api.soundcloud.com/me/activities/tracks/affiliated.json. The GET request works in the API console. Other GET requests work too, so I'm thinking that it's the definitely the way I'm asking for information in Xcode.
Here's the "Money" part of the program - gets the data, parses, creates a new view controller that has an array as a property with the response.
SCRequestResponseHandler handler;
handler = ^(NSURLResponse *response, NSData *data, NSError *error) {
NSError *jsonError = nil;
NSJSONSerialization *jsonResponse = [NSJSONSerialization
JSONObjectWithData:data
options:0
error:&jsonError];
if (!jsonError && [jsonResponse isKindOfClass:[NSArray class]]) {
UIStoryboard *sb = self.storyboard;
SCTTrackListViewController *trackListVC;
trackListVC = [sb instantiateViewControllerWithIdentifier:#"TLVC"];
trackListVC.tracks = (NSArray *)jsonResponse;
// Am I getting JSON back for tracks/affiliated.json ?
NSLog(#"json %#",(NSArray *)jsonResponse);
[self presentViewController:trackListVC
animated:YES
completion:nil];
}
};
NSString *resourceURL = #"https://api.soundcloud.com/me/activities/tracks/affiliated.json";
[SCRequest performMethod:SCRequestMethodGET
onResource:[NSURL URLWithString:resourceURL]
usingParameters:nil
withAccount:account
sendingProgressHandler:nil
responseHandler:handler];
}
If I use NSString *resourceURL = #"https://api.soundcloud.com/me/tracks.json"; or NSString *resourceURL = #"https://api.soundcloud.com/me/favorites.json"; then I get a list of tracks returned in JSON. If I use the one in the code example, I get no response - total silence.
The JSON response I get for https://api.soundcloud.com/me/favorites.json is in the form of
[
{
"kind": "track",
"id": 112547469,
"created_at": "2013/09/26 06:50:06 +0000",
"user_id": 294371,
"duration": 4499751,
"commentable": true,
"state": "finished",
"original_content_size": 143977742,
"sharing": "public",
"tag_list": "Funk Disco",
"permalink": "the-boogie-down",
"streamable": true,
"embeddable_by": "all",
"downloadable": false,
"purchase_url": null,
"label_id": null,
"purchase_title": null,
"genre": "Boogie",
"title": "The Boogie Down",
"description": "1.\tWar - The World Is A Ghetto (Suonho Edit)\r\n\r\n2. \tWhiskey Barons - The Same Love\r\n\r\n3.\tRayko - Broadway\r\n\r\n4.\tNicholas - Talking About Love\r\n\r\n5.\tBen E. King - Supernatural Thing (Fingerman Edit)\r\n\r\n6.\tGazeebo - Scaredy Cat\r\n\r\n7.\tFingerman - Fat Like You Know\r\n\r\n8.\tWillie Beaver - Party Time (Karim Edit)\r\n\r\n9.\tDeadly Sins - I Can Feel It\r\n\r\n10.\tFingerman - Tootin\r\n\r\n11.\tRocco Raimundo - Give Me Your Love\r\n\r\n12.\tLuther Vandross - Never Too Much\r\n\r\n13.\tKool & The Gang - Get Down On It (Rocco Raimundo Edit)",
"label_name": "",
"release": "",
"track_type": "",
"key_signature": "",
"isrc": "",
"video_url": null,
"bpm": null,
"release_year": null,
"release_month": null,
"release_day": null,
"original_format": "mp3",
"license": "all-rights-reserved",
"uri": "https://api.soundcloud.com/tracks/112547469",
"user": {
"id": 294371,
"kind": "user",
"permalink": "web_d",
"username": "webd",
"uri": "https://api.soundcloud.com/users/294371",
"permalink_url": "http://soundcloud.com/web_d",
"avatar_url": "https://i1.sndcdn.com/avatars-000011907556-1k1cgw-large.jpg?3eddc42"
},
"user_playback_count": 1,
"user_favorite": true,
"permalink_url": "http://soundcloud.com/web_d/the-boogie-down",
"artwork_url": "https://i1.sndcdn.com/artworks-000058671332-jzz9uc-large.jpg?3eddc42",
"waveform_url": "https://w1.sndcdn.com/qA5zJCmDqO46_m.png",
"stream_url": "https://api.soundcloud.com/tracks/112547469/stream",
"playback_count": 69,
"download_count": 0,
"favoritings_count": 1,
"comment_count": 0,
"attachments_uri": "https://api.soundcloud.com/tracks/112547469/attachments"
},
whilst the response for https://api.soundcloud.com/me/activities/tracks/affiliated.json goes:
{
"collection": [
{
"type": "track",
"created_at": "2013/10/08 20:14:16 +0000",
"origin": {
"kind": "track",
"id": 114421587,
"created_at": "2013/10/08 18:20:21 +0000",
"user_id": 144598,
"duration": 340616,
"commentable": true,
"state": "finished",
"original_content_size": 13624479,
"sharing": "public",
"tag_list": "",
"permalink": "faden-away",
"streamable": true,
"embeddable_by": "all",
"downloadable": false,
"purchase_url": null,
"label_id": null,
"purchase_title": null,
"genre": "Funk",
"title": "Faden Away",
"description": "",
"label_name": "",
"release": "",
"track_type": "",
"key_signature": "",
"isrc": "",
"video_url": null,
"bpm": null,
"release_year": null,
"release_month": null,
"release_day": null,
"original_format": "mp3",
"license": "all-rights-reserved",
"uri": "https://api.soundcloud.com/tracks/114421587",
"user": {
"id": 144598,
"kind": "user",
"permalink": "stonesthrow",
"username": "Stones Throw Records",
"uri": "https://api.soundcloud.com/users/144598",
"permalink_url": "http://soundcloud.com/stonesthrow",
"avatar_url": "https://i1.sndcdn.com/avatars-000001771161-2x04bn-large.jpg?3eddc42"
},
"user_playback_count": 1,
"user_favorite": false,
"permalink_url": "http://soundcloud.com/stonesthrow/faden-away",
"artwork_url": "https://i1.sndcdn.com/artworks-000059640743-qtexag-large.jpg?3eddc42",
"waveform_url": "https://w1.sndcdn.com/NBZ15f1BZ4cV_m.png",
"stream_url": "https://api.soundcloud.com/tracks/114421587/stream",
"playback_count": 29756,
"download_count": 0,
"favoritings_count": 1587,
"comment_count": 138,
"attachments_uri": "https://api.soundcloud.com/tracks/114421587/attachments",
"sharing_note": {
"text": "",
"created_at": "2013/10/08 20:14:16 +0000"
}
},
"tags": "affiliated"
},
{
"type": "track",
"created_at": "2013/10/08 14:26:00 +0000",
"origin": {
"kind": "track",
"id": 112820793,
"created_at": "2013/09/27 21:39:16 +0000",
"user_id": 1520490,
"duration": 290926,
"commentable": true,
"state": "finished",
"original_content_size": 12579855,
"sharing": "public",
"tag_list": ""MUSEUM OF LOVE" "DFA RECORDS" "PAT MAHONEY" "LCD SOUNDSYSTEM" "THE JUAN MACLEAN" JEE DAY DFA",
"permalink": "museum-of-love-monotronic",
"streamable": true,
"embeddable_by": "all",
"downloadable": false,
"purchase_url": "https://itunes.apple.com/album/monotronic-single/id711563768",
"label_id": null,
"purchase_title": "DIGITAL SINGLE",
"genre": "",
"title": "Museum of Love - Monotronic",
"description": "Museum Of Love\n"Monotronic"\n\nDFA2390\n\nfirst single "Down South" available here: http://store.dfarecords.com/products/dfa2389",
"label_name": "DFA Records",
"release": "DFA2390",
"track_type": "original",
"key_signature": "",
"isrc": "",
"video_url": "http://www.youtube.com/watch?v=K2E6oK7tN5w",
"bpm": null,
"release_year": 2013,
"release_month": 10,
"release_day": 8,
"original_format": "mp3",
"license": "all-rights-reserved",
"uri": "https://api.soundcloud.com/tracks/112820793",
"user": {
"id": 1520490,
"kind": "user",
"permalink": "dfa-records",
"username": "DFA Records",
"uri": "https://api.soundcloud.com/users/1520490",
"permalink_url": "http://soundcloud.com/dfa-records",
"avatar_url": "https://i1.sndcdn.com/avatars-000002067008-el39h6-large.jpg?3eddc42"
},
"user_playback_count": 1,
"user_favorite": false,
"permalink_url": "http://soundcloud.com/dfa-records/museum-of-love-monotronic",
"artwork_url": "https://i1.sndcdn.com/artworks-000058798982-ogzv3j-large.jpg?3eddc42",
"waveform_url": "https://w1.sndcdn.com/0m0pwUhjvenc_m.png",
"stream_url": "https://api.soundcloud.com/tracks/112820793/stream",
"playback_count": 10047,
"download_count": 0,
"favoritings_count": 309,
"comment_count": 18,
"attachments_uri": "https://api.soundcloud.com/tracks/112820793/attachments",
"sharing_note": {
"text": "",
"created_at": "2013/09/27 21:39:16 +0000"
}
},
"tags": "affiliated"
},
I'm trying to store the response in an NSArray, and I think that might be the issue... can someone point me in the right direction to get the JSON for affiliated.json? Is the response of the second JSON a dictionary? Do I need to store the response for that one differently? I thought I could store a dictionary in an NSArray...
The reason I was getting back nothing was due to this line: NSLog(#"json %#",(NSArray *)jsonResponse); where I was casting jsonResponse as an NSArray. This worked in the first instance because the response was an array, but in the second instance the response was a dictionary. Pretty obvious now that I think about it. I changed that line to (NSDictionary *)jsonResponse and all was well.
After I received the JSON data, this was also a good way to experiment with the different ways to dig into the nested info in the data. For instance, I created a dictionary to hold jsonResponse. I could get the info for the key I wanted in a couple of ways:
dictionary[#"user"][#"avatar_url] would get the nested value for avatar_url which was itself nested in user.
I could also do this by calling [dictionary valueForKeyPath:#"user.avatar_url"];

Relationship Mapping Restkit

I have two tables say Articles and Authors. Each Article have many authors and same author can be come in multiple Articles. My Json response is looks like this.
{
"Articles": [
{
"artName": "ABC",
"artId": "1",
"Authors": [
{
"autName": "James",
"autId": "200",
"email": "james#xyz.com"
},
{
"autName": "Mark",
"autId": "201",
"email": "mark#xyz.com"
},
{
"autName": "Robert",
"autId": "202",
"email": "robert#xyz.com"
}
]
},
{
"artName": "BCD",
"artId": "2",
"Authors": [
{
"autName": "James",
"autId": "200",
"email": "james#xyz.com"
},
{
"autName": "Ben",
"autId": "204",
"email": "ben#xyz.com"
},
{
"autName": "Rayon",
"autId": "205",
"email": "rayon#xyz.com"
}
]
}
]
}
Mapping:
RKObjectManager *manager = [[RestKit sharedDataManager] objectManager];
RKEntityMapping *ArticleMapping = [RKEntityMapping mappingForEntityForName:#"Articles" inManagedObjectStore:manager.managedObjectStore];
ArticleMaping.identificationAttributes = #[#"artId"];
ArticleMaping addAttributeMappingsFromDictionary:#{
#"artId": #"artId",
}];
RKEntityMapping *AuthorMapping = [RKEntityMapping mappingForEntityForName:#"Authors" inManagedObjectStore:manager.managedObjectStore];
AuthorMapping.identificationAttributes = #[#"autId"];
AuthorMapping addAttributeMappingsFromDictionary:#{
#"autId": #"autId",
#"autName" : #"autName",
#"email" : #"email"
}];
[ArticleMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"Authors" toKeyPath:#"Authors" withMapping:AuthorMapping]];
I have made a relationship for Articles and Authors. It is mapping correctly, but when i try to fetch the authors for "artName" ABC. i am getting only 2 authors. but when i try to fetch data for "artName" BCD. i am getting 3 authors. when checked in DB, relationship key for record "James" is overwritten by record of BCD.
How can i access all the authors for both the articles.
Note: I am using Restkit 0.20
Thank you
The relationship between the 2 objects needs to be many to many or the new assignment will explicitly break the old assignment.
When you create your RKRelationshipMapping, ensure that you set the assignmentPolicy to RKUnionAssignmentPolicy. The default is RKSetAssignmentPolicy which could be removing the existing setting.

Parsing Json to get all the contents in one NSArray

That's the content...
[
{
"id": "",
"title": "",
"website": "",
"categories": [
{
"id": "",
"label": ""
}
],
"updated":
},
{
"id": "",
"title": "",
"website": "",
"categories": [
{
"id": "",
"label": ""
}
],
"updated":
}
]
How can I insert every feed source in one array?
NSDictionary *results = [string JSONValue];
NSArray *subs = [results valueForKey:#"KEY"];
Which key I must insert?
THanks
as I can see your structure, you will get out of this JSON-String
NSArray:
[
NSDictionary:
{
NSString: "id",
NSString: "title",
NSString: "website",
NSArray: "categories":
[
NSDictionary:
{
NSString: "id",
NSString: "label"
}
],
NSNumber: "updated"
},
NSDictionary:
{
...
}
]
So you have already an array of "Feeds" at root and you have to itterate them with their index in the array with. For first id i.e. [[myJsonStructure objectAtIndex:0] objectForKey:#"id"];

Resources