cancel
Showing results for 
Search instead for 
Did you mean: 

SAPcpmsUserRoles returning nil - iOS native mobile service BTP

teddyfalsenhiis
Explorer
0 Kudos

Hi,

I'm trying to use SAPcpmsUserRoles to load user name, but experiencing that the return is nil.

I'm running this directly after the onboarding and therefore using the context of the sapURLsession.

My code is the following:

let settingsParameters = SAPcpmsSettingsParameters(backendURL: configurationURL, applicationID: appParameters["Application Identifier"] as? String)<br>var userRoles = SAPcpmsUserRoles(sapURLSession: context.sapURLSession, settingsParameters: settingsParameters)<br>userRoles.load { userInfo, error in<br>		print(userInfo?.givenName ?? "NO DATA»)<br>}

When i try to print settingsParameters, it returns destinations: nil:

SAPcpmsSettingsParameters(applicationID: HIDDEN, applicationVersion: Optional("1.0"), backendURL: https://mobile-services-free-tier-HIDDEN, destinations: nil, deviceID: Optional("HIDDEN"))

If I try to get the user data through GET request (as described here) I get all the data I need.

Help is much appreciated.

Accepted Solutions (1)

Accepted Solutions (1)

perage
Participant
0 Kudos

I fetch the user name this way:

username = try? userRepository.fetchUsername()
import SAPOfflineOData
import SAPFoundation
import SAPCommon

protocol UserRepository {
    func fetchUsername() throws -> String
}

class UserRepositoryImpl: UserRepository {
    
    private static let usernameKey = "username"
    
    private var sapUrlSession: SAPURLSession
    
    public enum UserRepositoryException: Error {
        case UserNameIsNotAvailableException
    }
    
    init(sapUrlSession: SAPURLSession){
        self.sapUrlSession = sapUrlSession
    }
    
    func fetchUsername() throws -> String {
        let userRoles = createUserRolesAccessInstance()
        
        var roleInfo: SAPcpmsUserRoles.SAPcpmsUserInfo?
        var error: Error?
        
        let dispatchGroup = DispatchGroup()
        dispatchGroup.enter()
        
        userRoles.load() { roleInfoResult, errorResult in
            roleInfo = roleInfoResult
            error = errorResult
            
            dispatchGroup.leave()
        }
        
        dispatchGroup.wait()
        
        return try handleUserRoleLoadingResult(roleInfo: roleInfo, error: error)
    }
    
    private func createUserRolesAccessInstance() -> SAPcpmsUserRoles {
        return SAPcpmsUserRoles(sapURLSession: sapUrlSession, settingsParameters: AppDelegate.shared.sessionManager.onboardingSession!.settingsParameters!)
    }
    
    private func handleUserRoleLoadingResult(roleInfo: SAPcpmsUserRoles.SAPcpmsUserInfo?, error: Error?) throws -> String {
        if let error = error {
            if let username = tryToGetUserNameFromCache() {
                return username
            }
            
            throw error
        }
        
        if let name = roleInfo?.userName {
            cacheUserName(userName: name)
            
            return name
        }
        else {
            throw UserRepositoryException.UserNameIsNotAvailableException
        }
    }
    
    private func cacheUserName(userName: String) {
        UserDefaults.standard.set(userName, forKey: UserRepositoryImpl.usernameKey)
    }
    
    private func tryToGetUserNameFromCache() -> String? {
        return UserDefaults.standard.value(forKey: UserRepositoryImpl.usernameKey) as? String
    }
    
}

Answers (0)