I know that when a user logs in, the data in the user table is fetched from the server. Whatever it had at the time when the user logged in is available. How do we get a “fresh” copy of the user object? For example, we want to get a fresh copy of the field “favoritedProducts” from the user table once the user has made changes to that field.
When we use the line of code below, it only returns the original data from the time the user logged in even though what is in the backendless console is different from what is being retrieved.
We are looking coding in swift 3.
let fav = backendless!.userService.currentUser.getProperty("favoriteProducts")
print("fav is \(fav)")
Hi Nevin,
You can retrieve a user using Data Service and get the properties you need from there. Will it suit your needs?
How would we do that? We need to get a string property of the currently logged in user (in our case the column name is “favoriteProducts”). This is in the user table. That string property constantly changed by the user but the user logged in many weeks ago. Right now when we use this code below, it returns the data that was in the favoriteProducts field when many weeks ago when the user logged in. We need the current string in “favoriteProducts” for that user.
backendless!.userService.currentUser.getProperty("favoriteProducts")
Just get an objectId value from currentUser and make a Data Service call to find the actual user by objectId.
Do you have any documents or a code snippet on doing that in swift 3.0? We have been struggling with it. I know it is extremely simple but for some reason we are doing something wrong.
If we want to print the string from the “favoriteProducts” column for the current user in the console, how would we do it?
You can refer to this doc page:
https://backendless.com/documentation/data/ios/data_basic_search.htm
I suppose the code would look something like this (sorry not sure since I’m not an expert in Swift):
let id = backendless!.userService.currentUser.getProperty("objectId")
let dataStore = backendless.data.of(BackendlessUser.ofClass());
let user = dataStore.findID( id );
let fav = user.getProperty( "favoriteProducts" );
Perfect. That worked. I thought I had tried that but I think I had made a mistake with converting the values. Here is the code that worked finally. Thanks for your help!
let id = backendless!.userService.currentUser.getProperty("objectId")
let dataStore = backendless?.data.of(BackendlessUser.ofClass())
let user = (dataStore?.findID(id) as AnyObject).getProperty("favoriteProducts")