Hello,
I have noticed that when I try to use the .getProperty on my BackendlessUser objects, sometimes it returns me nil even if there are some datas. For example in this function :
let bc = Backendless.sharedInstance().data.of(Participation.ofClass()).find(dataQuery, fault: &error)
if error == nil {
print("Participations have been found: \(bc.data)")
for object in bc.data as! [Participation] {
let event = object.eventID!
print(event.eventCreator)
}
}
When I call it for the first time, I get this result from the print :
Optional(<BackendlessUser> {
"___class" = Users;
city = "St-Saphorin-Morges";
country = Suisse;
created = "2016-03-14 08:47:43 +0000";
email = « test@icloud.com";
name = Nicolas;
objectId = "C035E32F-F3EE-3577-FF2C-13FAD5EF6400";
ownerId = "<null>";
password = "<null>";
picture = « ***";
role = admin;
surname = Me;
updated = "2016-03-16 10:36:48 +0000";
})
And when I call it for the second time, I get this result (no custom properties)
<BackendlessUser> {
email = « test@icloud.com";
name = Nicolas;
objectId = "C035E32F-F3EE-3577-FF2C-13FAD5EF6400";
}
Am I missing something here ? I’m just calling the function twice and I don’t get the same result.
Thanks !
Hi Nicolas,
We assigned a developer to investigate your issue. Please wait some time until we can give you any answer.
Regards,
Sergey
Hi Nikolas,
Please clarify: how did you get BackendlessUser objects, if you retrieved Participation.ofClass()) objects (see your code above)?
Can you add a whole sample code with used data classes (Participation, etc.) here?
Another way is to upload you project to Google Drive or Dropbox, and send the link to support@backendless.com. We will investigate it and let you know asap.
Regards,
Slava
Hello Vyacheslav,
Here is my logic and how I use BackendlessUser :
My app consists of a database of events that users can join and see what their friends are doing. First of all, when a user (logged in) indicates he’s joining an event, I am saving an object in my class « Participation »
class Participation : NSObject {
var objectId : String?
var createdAt : NSDate?
var userID : BackendlessUser?
var eventID : Event? }
So the userID will actually be the currentUser. Here is my code when saving the object (removed the irrelevant stuff) :
// Create participation
let participation = Participation()
participation.userID = backendless.userService.currentUser
participation.eventID = self.eventFull!
/* Save Participation */
let dataStoreParticipation = self.backendless.data.of(Participation.ofClass())
dataStoreParticipation.save(
participation,
response: { (result: AnyObject!) -> Void in
let obj = result as! Participation
print("Participation has been saved: \(obj.objectId)")
})
},
error: { (fault: Fault!) -> Void in
print("fServer reported an error: \(fault)")
})
So at this point, the object is correctly saved in my database and the user correctly linked. Next step is retrieving the participations of this user.
let whereClause = "userID.objectId = '\(currentUser.objectId)'"
let dataQuery = BackendlessDataQuery()
dataQuery.whereClause = whereClause
var error: Fault?
let bc = Backendless.sharedInstance().data.of(Participation.ofClass()).find(dataQuery, fault: &error)
if error == nil {
print("Participations have been found: \(bc.data)")
for object in bc.data as! [Participation] {
user = object.userID
let city = user.getProperty(« city ») as! String //Here I get nil
}
}
}
else {
print("Server reported an error: \(error)")
}
And here I have the problem. I get nil from the getProperty ! I use the very same logic at different places in my app and sometimes I get nil and sometimes not.
Ok, Nicolas,
When a data object or a collection of objects is retrieved using the client API, Backendless does not automatically include related objects into the returned. See this doc about retrieving the relations.
Here I fixed your code to solve the issue:
let whereClause = "userID.objectId = '\(currentUser.objectId)'"
let dataQuery = BackendlessDataQuery()
dataQuery.whereClause = whereClause
let queryOptions = QueryOptions()
queryOptions.related = ["userID"];
dataQuery.queryOptions = queryOptions
.......
Regards,
Slava
Thank you Slava It’s working now.
But I thought I didn’t have to do that because I checked the “autoload” boxes in my Backendless console…
Yes ! I’ve checked those boxes but I still need to use the queryOptions.related.
It is strange, the “autoload” works for me in my app.
Can you send appId & secretKey of your app to support@backendless.com? I check it.
I just checked your app with “autoloaded” UserID relation, and it works fine for me.
Here is my sample code:
func loginUser() {
Types.tryblock({ () -> Void in
let registeredUser = self.backendless.userService.login("spiday@backendless.com", password: "greeng0blin")
print("User has been logged in (SYNC): \(registeredUser)")
},
catchblock: { (exception) -> Void in
print("Server reported an error: \(exception as! Fault)")
})
}
func retrieveUserFromParticipation() {
self.loginUser()
let whereClause = "userID.objectId = '\(self.backendless.userService.currentUser.objectId)'"
let dataQuery = BackendlessDataQuery()
dataQuery.whereClause = whereClause
var error: Fault?
let bc = Backendless.sharedInstance().data.of(Participation.ofClass()).find(dataQuery, fault: &error)
if error == nil {
print("Participations have been found: \(bc.data)")
for object in bc.data as! [Participation] {
let user = object.userID as BackendlessUser!
let greetings = user.getProperty("greetings") as! String
print("GREETINGS = \(greetings)")
}
}
else {
print("Server reported an error: \(error)")
}
}
Here is a server response in Charles (you can see userID relation there):
http://support.backendless.com/public/attachments/0c817d53064911903b3a946b60e570bb.png</img>
and app log:
http://support.backendless.com/public/attachments/c0759dfd081c1c34a3795cc02c488af2.png</img>
Please check your code and compare with my sample, maybe the cause of the problem is some other things? Try my code, will it work for you?
Regards,
Slava
I just realized I still got that error randomly. Sometimes it works so my code should be correct ! I didn’t touched my code since yesterday and I get this error at random places where it wasn’t before.
Does those red lines mean something wrong ?
What error is that, Nicolas?
Red lines indicate that this is a one-to-many relation.
Mark
“unexpectedly found nil while unwrapping an optional value” when accessing BackendlessUser’s custom property even if added the queryOptions to add the related object. Now the error is gone again and appeared somewhere else.
Try to avoid mixing queryOptions.related with auto-load. Identify a policy for retrieving relations which would work the best for your app.
I’ve tried and it seems to work better now But I still have one problem, how do I load relation’s relations ?
For example I have an object which is linked with an other object which itself is linked to another entity. With Autoload I didn’t worried about that.
Use the dot notation in the queryOptions.related array: “foo.bar”, “person.address.city”
Thank you very much for all your answers ! Everything is ok now.