Backendless Support
 
Solved

Casting error

  1. let dataStore = self.backendless.data.of(Contact().ofClass())
  2. dataStore?.find(queryBuilder,
  3. response: {
  4. (sorted) -> () in
  5. let sortedArr = sorted as! [Contact] // Get an error here
  6. print("Result: \(sortedArr)")
  7. },
  8. error: {
  9. (fault : Fault?) -> () in
  10. print("Server reported an error: \(fault)")
  11. })

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

SDK: 4.0b3

Leave a Comment

Comments (15)

photo
1

Hi Brian,

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

Regards,

Mark

photo
1

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

photo
photo
1

Hello Brian,

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

  1. func sort () {
  2. let queryBuilder = DataQueryBuilder()
  3. queryBuilder!.setSortBy(["name"])
  4. let dataStore = self.backendless.data.of(Contact().ofClass())
  5. dataStore?.find(queryBuilder,
  6. response: {
  7. (sorted) -> () in
  8. let sortedArr = sorted as! [Contact]
  9. print("Result: \(sortedArr)")
  10. },
  11. error: {
  12. (fault : Fault?) -> () in
  13. print("Server reported an error: \(fault)")
  14. })
  15. }

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

photo
1

Contact class is added.

  1. class Contact: NSObject {
  2. var name: String?
  3. var addedField: Field?
  4. }

Field class

  1. class Field: NSObject {
  2. var foo: String?
  3. }

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

photo
1

Does it work if you disable the afterFind handler?

photo
1

Yes it does

photo
1

Could you please try the following:

  1. 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
  2. Then disable the event handler and repeat the request from REST Console
  3. Post both responses here, so we can take a look at them.

Mark

photo
1

Enabled:

  1. [
  2. {
  3. "created": "2017-03-18T07:42:03.000Z",
  4. "ownerId": "01BD69AA-B983-9174-FFE1-511496CE0700",
  5. "__meta": "{\"relationRemovalIds\":{},\"selectedProperties\":[\"created\",\"ownerId\",\"name\",\"___class\",\"updated\",\"objectId\"],\"relatedObjects\":{}}",
  6. "___class": "Contact",
  7. "name": "Brittney",
  8. "updated": "2017-04-03T20:30:46.000Z",
  9. "objectId": "20776383-929C-DD4B-FF4A-7CA921EE7300",
  10. "addedField": {
  11. "___class": "Field",
  12. "foo": "spmething"
  13. }
  14. }
  15. ]

Disabled:

  1. [
  2. {
  3. "created": 1489822923000,
  4. "ownerId": "01BD69AA-B983-9174-FFE1-511496CE0700",
  5. "__meta": "{\"relationRemovalIds\":{},\"selectedProperties\":[\"created\",\"ownerId\",\"name\",\"___class\",\"updated\",\"objectId\"],\"relatedObjects\":{}}",
  6. "name": "Brittney",
  7. "___class": "Contact",
  8. "updated": 1491251446000,
  9. "objectId": "20776383-929C-DD4B-FF4A-7CA921EE7300"
  10. }
  11. ]

photo
1

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

photo
1

The error still occurs.

photo
1

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

photo
1

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

photo
1

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

photo
photo
1

Hi Brian,

The problem has been fixed, please verify.

photo
1

It works. Thank you!

photo