Home > Enterprise >  put delay on firebase event in swift
put delay on firebase event in swift

Time:11-09

I am sending my product array to firebase, but I want to delay while doing this, at the same time I send my array in a split way. I want the part that says // this line below for 10 seconds, I can use a timer for this, but I don't know how to do it.

    func viewItemInList(items: [ProductModel]) {
    var parameters: [String:Any] = [:]
    var parameter = [parameters]
    
    let chunkedArray = items.chunked(into: 4)

    for array in chunkedArray {
        parameter.removeAll()
        for index in 0..<array.count  {
            let item = array[index]
            parameters = [  AnalyticsParameterItemID: item.sku ?? "",
                            AnalyticsParameterItemName: item.name ?? "",
                            //AnalyticsParameterItemCategory: categoryName,
                            //                                        AnalyticsParameterItemVariant: self.getColorFromWishList(wishListItem: item)
                            AnalyticsParameterItemBrand: "Skechers",
                            AnalyticsParameterPrice: item.price?.clearPrice() ?? 0.0
            ]
            parameters[AnalyticsParameterIndex] = index
            parameter.append(parameters)
        }
        
        parameters[AnalyticsParameterItems] = parameter
        **Analytics.logEvent(AnalyticsEventViewItemList, parameters: parameters)** //this line
    }
}

CodePudding user response:

You can check while you have reached the last element and then do your task. Here is your updated function-

      func viewItemInList(items: [ProductModel]) {
            var parameters: [String:Any] = [:]
            var parameter = [parameters]
            
            let chunkedArray = items.chunked(into: 4)

            for id in 0..<chunkedArray.count {
                parameter.removeAll()
                let tmpArray = chunkedArray[id]
                for index in 0..<tmpArray.count  {
                    let item = tmpArray[index]
                    parameters = [  AnalyticsParameterItemID: item.sku ?? "",
                                    AnalyticsParameterItemName: item.name ?? "",
                                    //AnalyticsParameterItemCategory: categoryName,
                                    //                                        AnalyticsParameterItemVariant: self.getColorFromWishList(wishListItem: item)
                                    AnalyticsParameterItemBrand: "Skechers",
                                    AnalyticsParameterPrice: item.price?.clearPrice() ?? 0.0
                    ]
                    parameters[AnalyticsParameterIndex] = index
                    parameter.append(parameters)
                    if (id == chunkedArray.words.count - 1) && (index == tmpArray.count - 1) {
                        parameters[AnalyticsParameterItems] = parameter
                        **Analytics.logEvent(AnalyticsEventViewItemList, parameters: parameters)** //this line
                    }
                }
            }
        }

CodePudding user response:

I used sleep and DispatchQueue.global

func viewItemInList(items: [ProductModel]) {
    var parameters: [String:Any] = [:]
    var parameter = [parameters]
    let chunkedArray = items.chunked(into: 4)
    
    DispatchQueue.global(qos: .default).async {
        for array in chunkedArray {
            parameter.removeAll()
            for index in 0..<array.count  {
                let item = array[index]
                parameters = [  AnalyticsParameterItemID: item.sku ?? "",
                                AnalyticsParameterItemName: item.name ?? "",
                                //AnalyticsParameterItemCategory: categoryName,
                                //                                        AnalyticsParameterItemVariant: self.getColorFromWishList(wishListItem: item)
                                AnalyticsParameterItemBrand: "Skechers",
                                AnalyticsParameterPrice: item.price?.clearPrice() ?? 0.0
                ]
                parameters[AnalyticsParameterIndex] = index
                parameter.append(parameters)
            }
            parameters[AnalyticsParameterItems] = parameter
            
            DispatchQueue.main.async {
                Analytics.logEvent(AnalyticsEventViewItemList, parameters: parameters)
            }
            
            sleep(10)
        }
    }
}
  • Related