Support Topics Documentation Slack YouTube Blog

Casting error

let dataStore = self.backendless.data.of(Contact().ofClass()) 
dataStore?.find(queryBuilder,
 response: {
 (sorted) -> () in
 let sortedArr = sorted as! [Contact] // Get an error here
 print("Result: \(sortedArr)")
},
 error: {
 (fault : Fault?) -> () in
 print("Server reported an error: \(fault)")
})

I’m getting an error for casting. Error: Could not cast value of type ‘__NSDictionaryM’ (0x000000) to ‘APPNAME.Contact’ (0x000000).

SDK: 4.0b3

Hi Brian,

Just to make sure, are you using it with an app on the 4.0 backend?

Regards,
Mark

Yes I am. In the response, “___ class” is set to null. Why is that?

Hello Brian,

I can’t reproduce this issue. Here is the method:

func sort () {
 let queryBuilder = DataQueryBuilder()
 queryBuilder!.setSortBy(["name"])
 let dataStore = self.backendless.data.of(Contact().ofClass())
 dataStore?.find(queryBuilder,
 response: {
 (sorted) -> () in
 let sortedArr = sorted as! [Contact]
 print("Result: \(sortedArr)")
 },
 error: {
 (fault : Fault?) -> () in
 print("Server reported an error: \(fault)")
 })
 }

The result attached as screen shot. I have no errors with 4.0b3.
Please make sure you have created the Contact class in your application.

Regards, Olga

Screen Shot 2017-04-13 at 3.50.58 PM.png

Contact class is added.

class Contact: NSObject {
var name: String?
var addedField: Field? 
}

Field class

class Field: NSObject {
var foo: String?
}

I have an afterFind() that adds a field to it. The other problem on top of ___class being null, is that the added field is not part of the json in the response from dataStore?.find

Does it work if you disable the afterFind handler?

Yes it does

Could you please try the following:

    Enable the event handler then go to Data > REST Console and run a GET request for the table which has the event handler. Record the JSON response Then disable the event handler and repeat the request from REST Console Post both responses here, so we can take a look at them.
Mark

Enabled:

[
    {
        "created": "2017-03-18T07:42:03.000Z",
        "ownerId": "01BD69AA-B983-9174-FFE1-511496CE0700",
        "__meta": "{\"relationRemovalIds\":{},\"selectedProperties\":[\"created\",\"ownerId\",\"name\",\"___class\",\"updated\",\"objectId\"],\"relatedObjects\":{}}",
        "___class": "Contact",
        "name": "Brittney",
        "updated": "2017-04-03T20:30:46.000Z",
        "objectId": "20776383-929C-DD4B-FF4A-7CA921EE7300",
        "addedField": {
            "___class": "Field",
            "foo": "spmething"
        }
    }
]

Disabled:

[
 {
 "created": 1489822923000,
 "ownerId": "01BD69AA-B983-9174-FFE1-511496CE0700",
 "__meta": "{\"relationRemovalIds\":{},\"selectedProperties\":[\"created\",\"ownerId\",\"name\",\"___class\",\"updated\",\"objectId\"],\"relatedObjects\":{}}",
 "name": "Brittney",
 "___class": "Contact",
 "updated": 1491251446000,
 "objectId": "20776383-929C-DD4B-FF4A-7CA921EE7300"
 }
]

So the differences I identified are:

When enabled:

  1. The format of the “updated” and “created” fields changed
  2. The “addedField” is added.

I suspect the first item is the root cause of the exception. To confirm this, would it be possible to create a no-op afterFind event handler (which does nothing) and see if the cast error still occurs?

Regards,
Mark

The error still occurs.

Thanks, Brian. This gives us enough info to reproduce the fix the problem. I opened an internal ticket for a developer.

Just to clarify - did you use a JS or Java event handler?

Thank you, Mark. It’s a JS event handler.

Hi Brian,

The problem has been fixed, please verify.

It works. Thank you!