CurrentUser behavior is very unstable IOS, Yesterday missed files from database, Internal error!!!

Internal server error with id EE56D86E-B977-8672-FFDE-2335E179C200 when i’m trying to open the table and it’s not for the first time
Also yesterday were destroyed all files from our database
And the problem which ruins all the logic of the app is the unstable behavior of the BackendlessUser
once it pass threw the function without any problem and the in other time it returns nil
And this behavior repeats in other functions, it is very-very buggy

Please provide me your appId, i will check this issue. What do you mean "i’m trying to open the table ", can you provide some code example?

i’m trying to open the table means that i’m trying to open the table in Backendless console

app id - EAD54513-0007-0178-FF17-C40D3CED4E00

Please clarify, what do you mean: “unstable behavior of the BackendlessUser”? Can you provideyour code which demonstrates the problem.

static func getUserDataFromBasicsSettings (array: [BasicSettings]) -> UserData? {

var userData: UserData?
print(currentUser.objectId) -> at this stage currentUser not nil

array.forEach({ (basicsSettings) in
print(currentUser.objectId) -> but in the loop currerntUser became nil

if basicsSettings.userData?.user?.objectId == currentUser.objectId {

userData = basicsSettings.userData!

return

}

})

return userData

}

and this situation repeats in different methods

i mean that once you login it load currentUser normally, but at other time it crashes at one of the different methods(not always in one method) were we use currentUser


Also when i delete user from database and try to register one more time it shows me the error
User Already Exists

here is code
@IBAction func FacebookLoginButton(sender: AnyObject) {
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logInWithReadPermissions([“public_profile”, “email”], fromViewController: self) { (result, error) -> Void in
if (error != nil) {
print(“error: (error)”);
} else if (result.isCancelled) {
print(“Cancelled”)
} else {
guard let token = FBSDKAccessToken.currentAccessToken() else {return}
Backend.getCurrentUser(token.userID, onSuccess: { (user) in
guard user != nil else {
FBSDKGraphRequest(graphPath: “me”, parameters: [“fields”: “id, name, first_name, last_name, picture.type(large), email”]).startWithCompletionHandler({ (connection, result, error) -> Void in
guard error == nil else {
print(error.description)
return
}
self.login(token)
})
return
}
let userId = user?.getProperty(“facebookId”) as? String
if userId == result.token.userID {
self.userDataOwnerId = user?.objectId
defaults.setObject(user?.objectId, forKey: self.currentUserObjectId)
defaults.setObject(token.userID, forKey: self.currentUsrFacebookId)
self.getUsersArray()
}
}, onError: { (errMsg) in
print(errMsg)
})
}
}

}

static func getCurrentUser(userFacebookId: String, onSuccess:(BackendlessUser?) -> (), onError:(Fault) -> ()) {
let dataQuery = BackendlessDataQuery()
dataQuery.whereClause = “facebookId = ‘(userFacebookId)’”
backendless.data.of(BackendlessUser.ofClass()).find(dataQuery, response: { (result) in
let userCollection = result.getCurrentPage()
if userCollection.count != 0 {
let currentUser = userCollection[0] as! BackendlessUser
onSuccess(currentUser)
} else {
onSuccess(nil)
}

    }) { (errMsg) in
        onError(errMsg)
    }
























}

here is debug i deleted user from base, deleted app from device and then registering the user

Here is error

user: <BackendlessUser [<BackendlessUser: 0x170201ac0>]> {

"___class" = Users;

"__meta" = "{\"relationRemovalIds\":{},\"selectedProperties\":[\"fb_email\",\"fb_last_name\",\"gender\",\"created\",\"fb_first_name\",\"facebookId\",\"fb_gender\",\"ownerId\",\"__updated__meta\",\"password\",\"geoLocation\",\"name\",\"___class\",\"id\",\"updated\",\"email\",\"objectId\"],\"relatedObjects\":{}}";

created = "2016-10-29 08:25:08 +0000";

email = "androidrivne@gmail.com";

facebookId = 1556805544624036;

"fb_email" = "&lt;null&gt;";

"fb_first_name" = Andro;

"fb_gender" = "&lt;null&gt;";

"fb_last_name" = Dev;

gender = male;

geoLocation = "&lt;null&gt;";

id = "&lt;null&gt;";

lastLogin = "2016-10-29 08:25:09 +0000";

name = "Andro Dev";

objectId = "3195B1C9-9B7B-E829-FF79-BEBD7FDADC00";

ownerId = "3195B1C9-9B7B-E829-FF79-BEBD7FDADC00";

password = "&lt;null&gt;";

updated = "&lt;null&gt;";

"user-registered" = 1;

"user-token" = "1A152477-8DD8-F15C-FFA9-13B4056F4C00";

}

Unable to register user. User already exists.

here is one more error

static func getUserData (id: String, onSuccess: (UserData) -> (), onError: (String) -> ()) {

    let dataQuery = BackendlessDataQuery()

    dataQuery.whereClause = "ownerid = '\(id)'"

    backendless.data.of(UserData.ofClass()).find(dataQuery, response: { (data) in

        let usrDat = data.getCurrentPage()

        guard usrDat != nil else {

            print("Backend.getUserData returns --> no user data")

            if let appDomain = NSBundle.mainBundle().bundleIdentifier {

                NSUserDefaults.standardUserDefaults().removePersistentDomainForName(appDomain)

            }

            return

        }

        let userData = usrDat[0] as! UserData

        onSuccess(userData)

    }) { (errMsg) in

        onError(errMsg.faultCode)

    }

}

look at the debug how much values returned request

data BackendlessCollection! 0x000000017027c1c0
ObjectiveC.NSObject NSObject
onSuccess Kafe1.UserData -> () 0x000000010017b7e4 Kafe1`partial apply forwarder for Kafe1.ContentViewController.(updateCurrentUserAndHisStatus () -> ()).(closure #1) at ContentViewController.swift
usrDat [AnyObject]! 12198008252293914598 values Some


userData Kafe1.UserData 0x000000017009d6f0

appDomain String

12198008252293914598 values - i think it is not good when i send request to get one value

I see that user with the email address of "androidrivne@gmail.com" is in the Users table. Can I delete the user and run the registration API call to see if I get the error?

Regards,
Mark

Please provide your UserData class (and all custom classes of its 1:1 relations if they are).

We need to reproduce this error with your data.

You need to use FB SDK version 4.15.1. The latest version was not working and returning token == nil. You also had to go to the Project Target and then Capabilities and switch Keychain Sharing ON. See the below issue.

http://stackoverflow.com/questions/39786577/facebook-sdk-login-doesnt-work-on-simulator-on-ios-10-xcode-8

Vyacheslav ,we have old version fb sdk and we getting token every time

Mark , yes you can , but notice that this error not arrives every time . It appears time to time

User data model

user data model.rtf (3.54kB)

here is the examples of the problems with backendlessUser

And what about files which disappeared in Thursday after problems with connection to database?

Hi Ios,

Here currentUser is really nil, because I think you only start to log in with Facebook SDK, and in that time user is not logged in:
http://support.backendless.com/public/attachments/8532a52d6a5455033e0c335d5780ac83.png</img>

Or do you use setStayLoggedIn:YES?

Please check: in all these cases (on screenshots) currentUser is nil or currentUser.objectId is nil?

I was logged earlier to the application and sure i called method userSetStayLoggedIn(true)
and also you see that user is nil in all other 4 view controllers

And what the difference of currentUser and currentUser.objectId
I need to have objectId to get user data from other tables by ownerId

For now i’m using NSUserDefaults to save user id and then get it from defaults, but i think it is not good.

here is my login controller


import UIKit
class ContentViewController: UIViewController {

let currentUsrFacebookId = “currentUsrFacebookId”
let currentUserObjectId = “currentUserObjectId”
var facebookId: String? = nil, userData: UserData?, userObjectId:String? = nil
var loadedInfo = loadedDataStatus.NotLoaded
var userSettings = BasicSettings
var status = true
var type = PushDealer.PushTypes.OnlineOffline.rawValue
var userDataOwnerId: String?

@IBAction func FacebookLoginButton(sender: AnyObject) {
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logInWithReadPermissions([“public_profile”, “email”], fromViewController: self) { (result, error) -> Void in
if (error != nil) {
print(“error: (error)”);
} else if (result.isCancelled) {
print(“Cancelled”)
} else {
guard let token = FBSDKAccessToken.currentAccessToken() else {return}
Backend.getCurrentUser(token.userID, onSuccess: { (user) in
guard user != nil else {
self.sendFacebookGraphRequest(token)
return
}
let userId = user?.getProperty(“facebookId”) as? String
if userId == result.token.userID {
self.userDataOwnerId = user?.objectId
defaults.setObject(user?.objectId, forKey: self.currentUserObjectId)
defaults.setObject(token.userID, forKey: self.currentUsrFacebookId)
self.getUsersArray()
}
}, onError: { (errMsg) in
guard errMsg.faultCode != “3064” else {
self.sendFacebookGraphRequest(token)
return
}
print(errMsg.detail)
})
}
}
}

func sendFacebookGraphRequest(token: FBSDKAccessToken) {
FBSDKGraphRequest(graphPath: “me”, parameters: [“fields”: “id, name, first_name, last_name, picture.type(large), email”]).startWithCompletionHandler({ (connection, result, error) -> Void in
guard error == nil else {
print(error.description)
return
}
self.login(token)
})
}

func login (token: FBSDKAccessToken) {
Backend.loginWithFacebook(token, OnSuccess: { (loggedUser) in
print(loggedUser.objectId)
defaults.setObject(loggedUser.objectId, forKey: self.currentUserObjectId)
defaults.setObject(token.userID, forKey: self.currentUsrFacebookId)
self.performSegueWithIdentifier(“ifFirstLoginInApp”, sender: nil)
}) { (errMsg) in
print(errMsg)
}
}
\Remark HERE IS STATIC FUNCTION WHICH I CALLED ABOVE
static func loginWithFacebook (token: FBSDKAccessToken, OnSuccess: (BackendlessUser) -> (), OnError: (String) -> ()) {
let fieldsMapping = [
“id” : “facebookId”,
“name” : “name”,
“birthday”: “birthday”,
“first_name”: “fb_first_name”,
“last_name” : “fb_last_name”,
“gender”: “gender”,
“email”: “email”
]
backendless.userService.loginWithFacebookSDK(token, fieldsMapping: fieldsMapping, response: { (newUser) in
backendless.userService.setStayLoggedIn(true)
OnSuccess(newUser)
}) { (errMsg) in
OnError(errMsg.detail)
}

}


@IBOutlet weak var FBLoginButton: UIButton! = {
let button = FBSDKLoginButton()
button.readPermissions = [“email”]
return button
}()

@IBOutlet weak var pageControl: UIPageControl!

var pageViewController: PageViewController! {
didSet {
pageViewController!.pageDelegate = self }
}

override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBarHidden = true
pageControl.addTarget(self, action: #selector(ContentViewController.didChangePageControlValue), forControlEvents: .ValueChanged)
if !userSignsUp {
guard /let fBookId = defaults.stringForKey(currentUsrFacebookId),/
let userId = defaults.stringForKey(currentUserObjectId) else {return}
userObjectId = userId
// facebookId = fBookId
updateCurrentUserAndHisStatus()
}
}

func updateCurrentUserAndHisStatus() {
Backend.getUserData(userObjectId!, onSuccess: { (userData) in
self.userData = userData
self.getUsersArray()
}) { (errCode) in

switch errCode {
case “-1009”:
DialogHelper.showOkAlert(“Please check your internet connection”, title: “The Internet connection appears to be offline”)
case “3064”:
if let appDomain = NSBundle.mainBundle().bundleIdentifier {
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(appDomain)
}
default: print(“Failed to get user data with code —> (errCode)”)
}
// guard errCode == “-1009” else { print(“Failed to get user data with code —> (errCode)”); return}
//
// DialogHelper.showOkAlert(“Please check your internet connection”, title: “The Internet connection appears to be offline”)
}
}

func getUserData() {
Backend.getUserData(self.userDataOwnerId!, onSuccess: { (userData) in
self.userData = userData
}, onError: { (errCode) in
guard errCode == “-1009” else { print(“Failed to get user data with code —> (errCode)”) ; return}
DialogHelper.showOkAlert(“Please check your internet connection”, title: “The Internet connection appears to be offline”)
})

}

func getUsersArray() {
Backend.getUsersArray { (loaded, dataNil, array) in
if !dataNil {
self.userSettings = array
if self.userDataOwnerId != nil {
Backend.getUserData(self.userDataOwnerId!, onSuccess: { (userData) in
self.userData = userData
self.updateUserDataAndsendPush()
}, onError: { (errCode) in
guard errCode == “-1009” else { print(“Failed to get user data with code —> (errCode)”) ; return}
DialogHelper.showOkAlert(“Please check your internet connection”, title: “The Internet connection appears to be offline”)
})

} else {
self.updateUserDataAndsendPush()
}
} else {
print(“Data of usersSettings = nil”)
}
}

}

func updateUserDataAndsendPush() {
self.userData!.deviceId = PushDealer.getUserDeviceId()
self.userData!.isOnline?.isOnline = true
Backend.updateUserdata(self.userData!, onSuccess: { (userData) in}, onError: { (errMsg) in print(errMsg)})
PushDealer.sendPushWithOnlineStatus(self.userSettings,/userData: self.userData!,/status: true,senderId: currentUserObjectId, type: self.type, message: onlineMessage)
self.performSegueWithIdentifier(“userLoggedSeque”, sender: self)

}

when you say it is disappeared do you mean, that it was removed from file service, or there no some records in table?

they were removed from file service

what files(full path) was removed?

Please check, if you set setStayLoggedIn(false) - will this issue persist?