2

How can I access the original response json data without using object mapping. I have the followingresponse data. it contains a nextSyncToken which used to execute the query and a collection of items (within the items session).

I created a object mapping of the Item object which represent the contents in items. However, I also need the nextSyncToken field. How can I access it without object mapping. Since the syncToken has no relationship with object mapping. How can i deal with this.

{
  "kind": "calendar#events",
  "nextSyncToken": "COib8eSw78gCEOib8eSw78gCGAU=",
  "items": [
    {
      "id": "_74rk4cpg84o42b9k8or3gb9k74s34b9p8ks34b9m851kac9m64rk4ci36g",
      "created": "2010-04-16T11:09:31.000Z",
      "updated": "2010-04-16T11:10:27.487Z",
      "summary": "iCal test 1",
      "start": {
        "dateTime": "2010-03-16T21:00:00+08:00"
      },
      "end": {
        "dateTime": "2010-03-16T22:00:00+08:00"
      }
    },
    {
      "id": "_752j2h1j6cq4cba68csjeb9k8p33eba1692k4ba284qj8ea688rj2chh6c",
      "status": "confirmed",
      "created": "2011-10-18T09:36:02.000Z",
      "updated": "2011-10-18T09:36:02.000Z",
      "summary": "New Event",
      "start": {
        "dateTime": "2011-10-18T03:45:00+08:00"
      },
      "end": {
        "dateTime": "2011-10-18T08:15:00+08:00"
      }
    }
  ]
}

My code of mapping:

    let eventMapping = RKEntityMapping(forEntityForName: "SGEvent", inManagedObjectStore: managedObjectStore)

    eventMapping.addAttributeMappingsFromDictionary([
        "id":               "identifier",
        "summary":          "summary",
        "created":          "createdAt",
        "updated":          "updatedAt",
        "location":         "location",
        "description":      "notes",
        "start.date":       "allDayStart",
        "end.date":         "allDayEnd"
        ])

    let startTimeMapping = RKAttributeMapping(fromKeyPath: "start.dateTime", toKeyPath: "startTime")
    startTimeMapping.valueTransformer = self.googleDateTransformer
    eventMapping.addPropertyMapping(startTimeMapping)

    let endTimeMapping = RKAttributeMapping(fromKeyPath: "end.dateTime", toKeyPath: "endTime")
    endTimeMapping.valueTransformer = self.googleDateTransformer
    eventMapping.addPropertyMapping(endTimeMapping)

    eventMapping.identificationAttributes = ["identifier"]

    let responseDescriptor = RKResponseDescriptor(mapping: eventMapping, method: .GET,
        pathPattern: "calendars/:calendarId/events", keyPath: "items",
        statusCodes: RKStatusCodeIndexSetForClass(RKStatusCodeClass.Successful))

    objectManager.addResponseDescriptor(responseDescriptor)

My request operation:

  objectManager.getObjectsAtPath("calendars/\(identifier)/events",
        parameters: [self.ACCESS_TOKEN: accessToken], success: { (operation, results) -> Void in



            callback?(results: nil, error: nil)
        }) { (_, error) -> Void in
            print(error)
    }
Joe SHI
  • 1,734
  • 4
  • 20
  • 39

2 Answers2

2

Generally you would add other response descriptors with appropriate mappings to deal with this issue.

When using objectManager.getObjectsAtPath you can get the raw data, assuming that you have some other response descriptor which will result in the success block being called, by navigating to the response data in the HTTP operation (which you can then unpack however you see fit):

operation.HTTPRequestOperation.responseData

(or use responseString instead of responseData).

Wain
  • 118,658
  • 15
  • 128
  • 151
1
 if let dict = try? NSJSONSerialization.JSONObjectWithData(operation.HTTPRequestOperation.responseData, options: .AllowFragments) as? [String: AnyObject],
      let nextSyncToken = dict?["nextSyncToken"] as? String{
    print(nextSyncToken)//get the nextSyncToken
}
wj2061
  • 6,778
  • 3
  • 36
  • 62