Start a new topic

NSInvalidArgumentException when calling refresh() and using custom class for user

Here's the deal. I'm trying to use custom class for users.


CustomUser.swift:


class CustomUser: User {

    @objc dynamic var stats: NSDictionary?

    @objc dynamic var address: String?

    

    @available(*, deprecated, message: "Please use Swift.Codable instead")

    override func mapping(map: Map) {

        super.mapping(map: map)

        

        stats <- ("stats", map["stats"])

        address <- ("address", map["address"])

    }

}


After initialization I try to check if saved user is actually there using refresh() function.


AppDelegate.swift:

Kinvey.sharedClient.userType = CustomUser.self

        

        Kinvey.sharedClient.initialize(

            appKey: "myAppKey",

            appSecret: "myAppSecret"

        ) {

            switch $0 {

            case .success(let user):

                if let user = user {

                    print("Active user found\(user)")

                    self.checkKinveyUser()

                } else {

                    print("user not found")

                    self.autogenerateNewUser()

                }

            case .failure(let error):

                print("\(error)")

            }

        }


Kinvey.sharedClient.activeUser?.refresh() { result in

            switch result {

            case .success:

                print("User is active and up to date!\n Sending notification \(FirstViewController.userLoggedInNotification)")

                NotificationCenter.default.post(name: FirstViewController.userLoggedInNotification, object: nil)

            case .failure(let error):

                print("AppDelegate:checkKinveyUser(): \(error)")

                self.autogenerateNewUser()

            }

        }


But then I get an error

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull _fastCStringContents:]: unrecognized selector sent to instance 0x1b7f319a0'

Using iOS SDK 3.19.0 and xCode 10.0 (10A255), along with Swift 4.2. iPhone 8 running iOS 12.


Hello,


The error you mentioned is not related to Kinvey but still, I have started looking into this problem. I had implemented 'Refresh()' function a few days ago and didn't see any issues. I will get back to you once I have an update.


Thanks,

Pranav


1 person likes this

Hi Vlad,


Apologies for not getting back to you earlier.


Please try removing "@objc dynamic" from the custom user model class for all the attributes/ properties. Also, replace the use of NSDictionary by Dictionary i.e. use "var stats: Dictionary<String, String>?" instead of using "var stats: NSDictionary?". After making these modifications, just rebuild-> run the project and let me know if it fixes the issue.


 If the above suggestion doesn't help, then please provide the following information: 

  • The complete stack trace of the crash from Xcode's debug area
  • Custom user model class definition



Thanks,

Pranav

Kinvey

Hello, Pranav.


Yes, the problem goes away if I comment those lines out.


Anyway, things got strange.

Everything worked for the past few days. I was able to parse data and modify it, but then the problem is back.


Error I'm seeing after calling refresh func:

2018-10-17 02: 28: 20.008547 + 0700 (appname) [27288: 5466408] - [NSNull count]: unrecognized selector sent to instance 0x1b7f319a0
2018-10-17 02: 28: 20.010441 + 0700 (appname) [27288: 5466408] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [NSNull count]: unrecognized selector sent to instance 0x1b7f319a0'

 

Still trying to figure out how to solve this permanently. Maybe I should override refresh() method? If so, how exactly should I do that?

Thanks in advance,

Vlad.

Hello,


Still waiting for your input.


Much appreciated,

Vlad

Hello Vlad,


Can you please tell me which line of code is causing this issue? NSNull is getting passed to an object and that's why it is crashing or you are calling a method on an object that doesn't respond to that method. Please apply breakpoints in your project and let me know the line at which it is crashing.


Also, if you remove or comment all references of "stats" dictionary then does it fix the issue? Comment the following lines:


  • @objc dynamic var stats: NSDictionary?
  • stats <- ("stats", map["stats"])



Thanks,

Pranav

Kinvey




Login or Signup to post a comment