Start a new topic
Answered

iOS SDK 3.0 Bug - custom class for User isn't instantiated

I am using iOS sdk 3.0. I have created a custom user sublclass as explained in http://devcenter.kinvey.com/ios-v3.0/guides/users#CustomAttributesCustomUserObject


below is my SDK initializatin code in appDelegate:


aKinvey.sharedClient.userType = CustomKinveyUser.self

       Kinvey.sharedClient.initialize(

            appKey: "kid_xxxxx",

            appSecret: "my-app-secret"

        )


The CustomKinveyUser class looks like:


class CustomKinveyUser: Kinvey.User {

    var profilePicURL: String?

    override func mapping(map: Map) {

        super.mapping(map: map)

        profilePicURL <- map["profilePicURL"]

    }

}


At some point in the app, after I have a logged-in session,  I am doing this - 

 guard let user = Client.sharedClient.activeUser as? CustomKinveyUser else {return}

 

The above line works and user is non-nil and is of type CustomKinveyUser

if and only if I have signed in during that particular launch of the app.


However, in subsequent app launches if I  already have a signed-in session, Client.sharedClient.activeUser returns a non-nil object as expected, but the class type is the base user class (Kinvey.User) instead of CustomKinveyUser and so the downcast fails (i.e the as? CustomKinveyUser part returns nil) 


captured from the xcode console:


(lldb) po Client.sharedClient.activeUser

▿ Optional<User>

  ▿ some : <__KNVUser: 0x7fdf34e829d0>




Best Answer
Niraj,

Thanks for the detailed explanations. Engineering is looking at the issue.

We'd like you to know that our SDK is open source and we are happy to accept contributions. If you have a solution for this issue or any others in the future, please feel free to submit a pull request to our repo. Our team will gladly review it and accept your fix.

 

Thanks,

Pranav

Kinvey


Sagar,


Can you try the fix that Niraj has mentioned above?


Thanks,

Pranav

Kinvey

 

Hi All,


Nirajs fix does indeed work...


However it may be a better idea to extend the Kinvey.Client Class like so:

extension Kinvey.Client {

 var activeCustomUser: CustomUser? {
   get {
     if let user = Kinvey.sharedClient.activeUser {
       let map = Map(mappingType: .fromJSON, JSON: user.toJSON())
       let customUser = CustomUser.init(map: map)
       return customUser
   } else {
     return nil
   }
 }
 }

This has two advantages, it firstly decouples from the Kinvey SDK thus pod updates won't break your code.

  

Personally I have extended the UIViewController Class, the reason being, all ViewControllers display different content based on the user being logged in or not.


extension UIViewController {

 var activeUser: CustomUser? {
   get {
     if let user = Kinvey.sharedClient.activeUser {
       let map = Map(mappingType: .fromJSON, JSON: user.toJSON())
       let customUser = CustomUser.init(map: map)
       return customUser
   } else {
     return nil
   }
 }
 }

 

Sagar,

We'd like you to know that our SDK is open source and we are happy to accept contributions. If you have a solution for this issue or any others in the future, please feel free to submit a pull request to our repo. Our team will gladly review it and accept your fix.

Thanks,
Pranav
Kinvey

 

Hi All,


The fix suggested I have found to not work when calling customUser.logout(), nothing happens.

It seems like Nirajs and Sagar Patel's solution no longer works with the latest 3.5.0 SDK.


Does anyone else have a solution?

Even with the latest iOS Kinvey SDK version 3.5.0, this problem still exists....that is, getting the Kinvey.sharedClient.activeUser still  doesn't return the custom User object if the app is killed (only works after a fresh login).

Chris,

This issue has already been escalated to engineering earlier this week and will update you once I get more information from engineering.

Thanks,
Pranav
Kinvey

MLIBZ-1800

Chris,


We are pleased to inform you that a new release of the iOS SDK v3.5.1 has been posted and is available for download at the following URL.

 

http://devcenter.kinvey.com/ios/downloads

 

This update fixes the problem described in this ticket regarding CustomClass that you discovered in the 3.5.0 SDK.

 

At your convenience, please download and test the fix and let us know if it works to your satisfaction.


Thanks,

Pranav

Kinvey

Hi all,

I found another temporary solution to solve this cases especially if you want to load custom viewController depend on the logging status 

Simply open "User.swift" class and add your own properties and remove this from your app delegate 

"sharedClient.userType = CustomUser.self"  in my case I did that 

1 - open Kinvey User Class and add following 

create your variables like "open var fullname :String?" 

2- Edit the mapping function like 

 open func mapping(map: Map) {

        _userId <- map[PersistableIdKey]

        acl <- map[PersistableAclKey]

        metadata <- map[PersistableMetadataKey]

        socialIdentity <- map["_socialIdentity"]

        username <- map["username"]

        email <- map["email"]

        fullname <- map["full_name"] // as you want 

    }

    3- Build your project 

It works for me like charm 


conclusion because Kinvey is open source framework so edit it as you like.

Hope this help 

Thanks 

Login or Signup to post a comment