Support Topics Documentation Slack YouTube Blog

Conditional addCreateListener

Hi

I want to implement a conditional create listener that triggers when a related object (of the main object) met a condition.

I have this table called DBSolicitudes.

And I have this code to create a BDSolicitudes object:

static func RequestAddDTMTeam( RecID_DTM: String, equipo: String, completion: @escaping (Result< Bool, Error>) -> ()){
        let request = BDSolicitudes()
        Backendless.shared.data.of(BDSolicitudes.self).save(entity: request,
            responseHandler: { result in
                let solicitud = result as! BDSolicitudes
                let dataStore2 = Backendless.shared.data.of(BDSolicitudes.self)
                dataStore2.setRelation(columnName: "Usuario", parentObjectId: solicitud.objectId!, childrenObjectIds: [RecID_DTM], responseHandler: { relations in
                    dataStore2.setRelation(columnName: "Equipo", parentObjectId: solicitud.objectId!, childrenObjectIds: [equipo], responseHandler: { relations in
                        completion(.success(true))
                }, errorHandler: {fault in
                    completion(.failure(fault))})
            }, errorHandler: {fault in
                completion(.failure(fault))})
        }, errorHandler: {fault in
            completion(.failure(fault))})
    }

On the other hand, I have the following code so, the user related to the BDSolicitudes object can process the request.

func configurarsubscripciones(usuId: String) {
        let eventHandler = Backendless.shared.data.of(BDSolicitudes.self).rt
        let cuando = "Usuario.objectId = '\(usuId)' "
        _ = eventHandler?.addCreateListener(whereClause: cuando, responseHandler: { createdObject in
            let sol = createdObject as! BDSolicitudes
            print(NSLocalizedString("You have bee requested to join a new team", comment: ""))
            print ("cambio en la tabla solicitudes")
            FuncionesBackendless.BuscaEquipoID (ID_Equipo: sol.Equipo!.objectId!) {resultados in
                switch (resultados){
                case .success(let equipo):
                    DispatchQueue.main.async {
                        self.equipoActivo = MVDTM (NuevoEquipo: equipo,  esSM: false, esReal: true)
                        self.SelMenEqu = .Miembros
                        self.hayAlerta = .Invitacion}
                case .failure(let error):
                    print (error.localizedDescription)
                }
            }
        }, errorHandler: { fault in
            print("Error: \(fault.message ?? "")")
        })}

My problem is that the listener is not triggered. The reason should be that the User relation has not been set yet at the moment of the object creation.

I tried as well using addUpdateListener but (I don’t know why), but It is not triggered.

If I use an Unconditional Delivery Listeners, it does work, so think it is not an issue with the listener setup.

Does anyone an idea on how to manage this?

Thank you for your help in advance.

Hi @Alejandro_Cuartas_Fernandez

We will take care of your case and respond as soon as possible

Regards, Igor

1 Like

Hello,

As far as creating object is happening separately from setting the relation you cannot handle object creation with condition connected to the related table.

To handle setting the relation please use one of these methods:

func addSetRelationListener(relationColumnName: String, responseHandler: ((RelationStatus) -> Void)!, errorHandler: ((Fault) -> Void)!) -> RTSubscription?

func addSetRelationListener(relationColumnName: String, parentObjectIds: [String], responseHandler: ((RelationStatus) -> Void)!, errorHandler: ((Fault) -> Void)!) -> RTSubscription?

func addSetRelationListener(relationColumnName: String, parents: [[String : Any]], responseHandler: ((RelationStatus) -> Void)!, errorHandler: ((Fault) -> Void)!) -> RTSubscription?

func addSetRelationListener(relationColumnName: String, customParents: [Any], responseHandler: ((RelationStatus) -> Void)!, errorHandler: ((Fault) -> Void)!) -> RTSubscription?

Regards,
Olha

Hi Olha,

Thank you for reaching out.

I have updated my func accordingly:

func configurarsubscripciones(usuId: String) {
        let eventHandler = Backendless.shared.data.of(BDSolicitudes.self).rt
        _  = eventHandler?.addSetRelationListener(relationColumnName: "Usuario", parentObjectIds: [usuId], responseHandler: { createdObject in
            print(NSLocalizedString("You have bee requested to join a new team", comment: ""))

//my code to add a new team member
}, errorHandler: { fault in
print(“Error: (fault.message ?? “”)”)
})
}

Unfortunately the responseHandler is not triggered.

Can you please help me to find where I made the mistake?

Than you in advance for your time

Best regards

Socket requires several seconds to establish connection, maybe it is not established before you try to set the relation?
Also please notice that the parentObjectIds parameter should be the array of your DBSolicitudes object’s ids, not the Users one.

Could you also provide your APP ID?

Than you again, I was using the Users’ objectId,

As I do not have the DBSolicitudes object by the time I create the listener, I can’t use this solution. I will try to find a workaround,

Thank you for clarifying.

Best regards