RestKit MongoDB location data mapping - ios

I am having trouble understanding how to Map location Data stored in Mongo DB with RestKit.
Here is the data I will be mapping in JSON
{ "name" : "TestPoint2", "media_resource" : "tester", "added" : ISODate("2012-10-10T23:00:00Z"), "loc" : { "type" : "Point", "coordinates" : [ -33.1, 12.54 ] }, "comments" : [ ], "tags" : [ ]}
The main point I am struggling with is mapping the "coordinates" of "loc"

To begin with, I'd treat the coordinates as they are represented in the JSON and store them as numbers in an array. If your object / entity has an array property / transformable attribute then you can just map the coordinates into that wholesale. You can then add accessor methods / transient attribute(s) which access that coordinate information and provide a nicer interface to get the lat/long than indexing into an array.

Related

Firebase Database & Swift: Save location data in an array

Hi all I am having a hard time saving coordinates in an array on Firebase. Basically I would like to track my location and save the Latitude and Longitude whenever there is a change in location.
This is what I currently get:
"-R359orfmkXiwERe948fk" : {
"coordinates" : {
"-S6O96394fXd0489fj" : {
"lat" : Lat1,
"long" : Long1
},
"-S6496SX235Hh12G893sb" : {
"lat" : lat2,
"long" : long2
},
"-F6O3941SwKZ5cA29038kdsg" : {
"lat" : lat3,
"long" : lat3
},
While This is the form I would like to get:
"-385fjAhm85fwERe948fk" : {
"coordinates" : [ [ Lat1,Long1], [Lat 2, Long2], [Lat 2, Long3]]
Using Firebase RealTime database and Swift 3
Firebase has no native support for arrays.
Instead of expecting Firebase to return you an array, I suggest writing a transformation method for your Swift model object, that you can use when you query Firebase, which converts the dictionary you currently receive into an array that you can then utilize throughout your app.
You will need a similar transformation that will then convert your array back to that dictionary format for updating Firebase.

Mapping a Dictionary to a RestKit result

In a JSON model coming from a REST api I have a field that is supposed to be a Dictionary (a map, hashtable, call it whatever you want), e.g.:
"moduleMap": {
"677e55b2-d80c-4b32-bcbb-e99074fbfcf6": {
"id": "677e55b2-d80c-4b32-bcbb-e99074fbfcf6",
"startTime": 1496054599227,
"status": "ACTIVE"
},
"c20acde2-639f-4cb7-9b90-6d8d24c78166": {
"id": "c20acde2-639f-4cb7-9b90-6d8d24c78166",
"startTime": 1496054598997,
"status": "UNAVAILABLE"
}
}
As I understand RestKit (I am a newbie), usually this is mapped to an object in Swift. However, since the keys in this map are arbitrary uuids, I cannot write a class with these properties.
Can anybody point me to a direction of how to get this map into a Swift dictionary using RestKit mapping?
I would like to map it to var moduleMap: [String:DomainObject], or at least var moduleMap: NSMutableDictionary. Also, I need it to be mappable back to the same JSON.

How to handle object list request

I am trying to read a list of objects
[
{
"name" : "Jhone",
"age" : 25
},
{
"name" : "Chris",
"age" : 24
}
]
How to handle that types of request?
You can loop through the array to access each object by its key. Assuming that array of objects is set to a variable called array
array.each { |element| puts element["name"] }
Btw, the objects are incorrectly formatted. You need a comma after each name value.

How to extract data from JSON with Alamofire

I use the Alamofire library this way:
Alamofire.request(.POST, "http://www.somesample.com/getData.php", parameters: ["user":"charles"]).responseJSON{jsonData in
var theData = jsonData.result.value
If I debug print the theData variable, it throws something like:
[
{
"userId" : "61",
"userPicture" : "147884767502.jpg",
"wasId" : "80",
"favorite" : "0",
"message" : "how are you?",
"username" : "paco",
"date" : "13\/10\/2015 03:44PM",
"userPhrase" : "hello"
"repliesNumber" : 2
},
{
"userId" : "3",
"userPicture" : "149181897286.jpg",
"wasId" : "5",
"favorite" : "0",
"message" : "let's go!",
"username" : "loliFlower",
"date" : "30\/08\/2015 07:48PM",
"userPhrase" : "ciiiii!",
"repliesNumber" : 3
}
]
I usually use SwiftyJSON so I write (even I use a for loop to walk to every single index of the array the SwiftyJSON makes:
var myData = JSON(theData.result.value)
print(myData[0]["username"].stringValue)
But what if I don't want to use SwiftyJSON library anymore? what is the native way to do this?
I recommend that you use code generation to create native models out of the json response, this will save your time of parsing by hand and reduce the risk of errors due to mistaken keys, all elements will be accessible by model properties, this will be purely native and the models will make more sense rather checking the keys. Check http://www.json4swift.com and let me know if you require further help in initiating the object from your json response. (make sure you enter the actual json response and not the printed object as that in your question)

RestKit JSON Mapping

I have given JSON and cannot parse partial data. It seems dictionary into dictionary:
{
"products": [
{
"id": 6796,
"title": "my title",
"description": "desc",
"code": "12345",
"valueType": "null",
"discounts": [
{
"minPrice": null,
"maxPrice": null,
"value": 20,
"avail": false
}
]
}
]
}
I am using the latest version of RESTKit but I cannot properly parse under discounts.
my RestKit settings are:
responseMapping.addAttributeMappingsFromDictionary([
"id" : "id",
"code" : "code",
"title" : "title",
"valueType" : "valueType",
"description" : "desc",
"discounts.minPrice" : "minPrice",
"discounts.maxPrice" : "maxPrice",
"discounts.value" : "value",
"discounts.avail" : "avail",
])
but all values below discounts always return Nil. What I am doing wrong?
You can't directly map using discounts.XXX because discounts is an array and you have no way to index into that array and extract a single value.
You either need to change the source JSON to compress the values out of the dictionary, or create a custom object that you can map each item in the discounts array to.
Technically you could map the whole discounts array, which would give you an array of dictionaries, that you could then unpack in the setter method, but the array of custom objects is usually a better approach.

Resources