Relation autoload not working

Since I have migrated to Backendless 4.0 my table’s relations that are checked to autoload are not working.

I have solved it in DataQueryBuilder using

query!.addRelated("RelationName")

But when I save the data and receive the class in response the relations don’t load in the result.

It worked in Backendless 3.0

Well, so I need to Fetch the data after I save it?

Relations were saved differently in 3.0

Ok, so one thing more to increase the number of requests. This way I think it’s going to become too expensive to use Backendless for a startup app.

The original question of this topic “relation auto-load is not working”. There were absolutely no changes related to data retrieval. To be honest, I do not understand what the problem you reported is.

The problem is that I have relations in my table that are checked to autoload and it is not happening unless I explicit it to DataQueryBuilder

Try setting relationDepth to 0

This is my function and it still returns me null to the relation filed. The fields that belong to the class return normally.

//Salva os dados do perfil no Backendless
    func salvarDadosPerfil() {
        
        let dados = backendless!.data.of(Usuario.ofClass())
         
        dados!.save(usuario,
            response: { (result: Any?) -> Void in
                       
                let usuarioRetorno = result as! Usuario
                
                let query = DataQueryBuilder()
                query!.setWhereClause("objectId = '\(usuarioRetorno.objectId!)'")
                query!.addRelated("pais")
                let usuarioBackendless = self.backendless?.persistenceService.of(Usuario.ofClass()).find(query!) as! [Usuario]
                
                print(usuarioBackendless[0].nome!)
                print(usuarioBackendless[0].pais?.nome)


                self.usuario = usuarioBackendless[0]
                
                if self.fotoSelecionada {
                    self.uploadFotoPerfil()
                } else {
                    self.salvarInfoDispositivoUsuario()
                }
                        
                //print("Usuário has been saved: \(usuarioRetorno.objectId)")
                        
                //self.indicadorDeAtividade.parar()
                        
        }, error: { (fault: Fault?) -> Void in
                    
            /*if self.fotoSelecionada {
                self.deletaFotoPerfil(self.usuario!.foto!)
            } else {
                UserDefaults.standard.removeObject(forKey: "iDoPartyUsuario")
                        
                self.indicadorDeAtividade.parar()
            }*/
                    
            //print("Server reported an error to save the User: \(fault)")
            
            self.indicadorDeAtividade.parar()
            
            self.mostrarErro(0, mensagemDoErro: "There was an error saving the profile")
            
        })
    }

Add the code to set relationDepth to 0. It should be done on DataQueryBuilder.

No, it didn’t work. It is returning null yet.

I am using addRelated. And if I use setRelated?

Can you show the code where you added relationDepth? (And confirm that THAT code doesn’t work)

Here it goes

let query = DataQueryBuilder()
                query!.setWhereClause("objectId = '\(usuarioRetorno.objectId!)'")
                query!.addRelated("pais")
                query!.setRelationsDepth(0)
                print(usuarioRetorno.objectId!)
                let usuarioBackendless = self.backendless?.persistenceService.of(Usuario.ofClass()).find(query!) as! [Usuario]

Hi Guilherme,

I’ve createad an internal task (ID BKNDLSS-15379) to investigate the issue. We shall notify you as soon as we have any news.

Hello,
This issue has been fixed. Please check the newest version of iOS-SDK (4.0.3).

Regards, Olga

I have download the new iOS SDK (4.0.3) and now when I run my app a method that was fully working is not working anymore.

//Carrega dados dos países
    func carregaDadosPaises() {
        
        let tamanhoPagina: Int32 = 100
        
        let query = DataQueryBuilder()
        query!.setSortBy(["nome"])
        query!.setPageSize(tamanhoPagina)
        
        buscaPaisesBackendless(query: query!)
    }
    
    func buscaPaisesBackendless(query: DataQueryBuilder) {
        
        indicadorDeAtividadeAtivo = true
        
        backendless!.persistenceService.of(PaisCodigo.ofClass()).find(
            query,
            response: {
                (paisesBackend) -> () in
                
                let resultado = paisesBackend as! [PaisCodigo]
                
                print(resultado.count)
                
                if resultado.count > 0 {
                    for paisBackend in resultado {
                        self.paises.append(paisBackend)
                    }
                    self.buscaPaisesBackendless(query: query.prepareNextPage())
                } else {
                    self.indicadorDeAtividadeAtivo = false
                }
            },
            error: { (fault : Fault?) -> () in
                //print("Server reported an error: \(fault)")
                
                self.mostrarErro(1, mensagemDoErro: "There was an error loading the data")
            }
        )
    }

Where I put the print(resultado.count) keeps returning me always 10 results e doesn’t leave the looping.

Sorry, Please return on 4.0.2 and add

query.setRelationsDepth(1)

to your code to load the relations.

Regards, Olga

Well, now I got another error with another method that was working before.

I am receiving this error:
Server reported an error to save the User: Optional(FAULT = ‘Server.Processing’ [exception during method invocation] <exception during method invocation> )

when I run this method:

//Salva os dados do perfil no Backendless
    func salvarDadosPerfil() {
        
        let dados = backendless!.data.of(Usuario.ofClass())
         
        dados!.save(usuario,
            response: { (result: Any?) -> Void in
                       
                let usuarioRetorno = result as! Usuario
                       
                //self.usuario!.objectId = resultado.objectId
                //self.usuario!.created = resultado.created
                //self.usuario!.updated = resultado.updated
                //self.usuario!.ownerId = resultado.ownerId
                
                let query = DataQueryBuilder()
                query!.setWhereClause("objectId = '\(usuarioRetorno.objectId!)'")
                query!.addRelated("pais")
                query!.setRelationsDepth(0)
                print(usuarioRetorno.objectId!)
                //let usuarioBackendless = self.backendless?.persistenceService.of(Usuario.ofClass()).find(query!) as! [Usuario]
                let usuarioBackendless = self.backendless?.persistenceService.of(Usuario.ofClass()).find(byId: usuarioRetorno.objectId!, queryBuilder: query) as! Usuario
                
                print(usuarioBackendless.nome!)
                print(usuarioBackendless.pais!.nome!)
                print("Imprimiu")
                self.usuario = usuarioBackendless
                
                if self.fotoSelecionada {
                    self.uploadFotoPerfil()
                } else {
                    self.salvarInfoDispositivoUsuario()
                }
                        
                //print("Usuário has been saved: \(usuarioRetorno.objectId)")
                        
                //self.indicadorDeAtividade.parar()
                        
        }, error: { (fault: Fault?) -> Void in
                    
            /*if self.fotoSelecionada {
                self.deletaFotoPerfil(self.usuario!.foto!)
            } else {
                UserDefaults.standard.removeObject(forKey: "iDoPartyUsuario")
                        
                self.indicadorDeAtividade.parar()
            }*/
                    
            print("Server reported an error to save the User: \(fault)")
            
            self.indicadorDeAtividade.parar()
            
            self.mostrarErro(0, mensagemDoErro: "There was an error saving the profile")
            
        })
    }

Is this happening with 4.0.2? There shouldn’t be any error because there were no changes with saving method at all.

It’s happening with 4.0.2

Ok, we’ll investigate this issue and answer as soon as possible. What version of 4.0 SDK did you use before?