-2

How do I make function that create delay in for loop what works on another thread so that main thread will not affects. Let suppose I have 100s of data in my array and I want to pick data from array one by one and submit that data to process in 1-2 seconds of delay and Main thread should not be affected.

How do i achieve this, I tried many solution but they are not properly working

private func synchDataWithCloudIfAvailable() {

    let arrUrl = SwiftFileManager.getListOfFileURLFromDictory(dicrectoryName: KSensor_Directory)

    if arrUrl != nil {

        if arrUrl!.count > 0 {

            self.diskOperationSerialQueue?.async(execute: {

                for url in arrUrl! {

                    let blockOperation = BlockOperation()

                    blockOperation.addExecutionBlock({

                        let data = SwiftFileManager.getDataFromFileUrl(url: url)
                        if data != nil {

                            //self.uploadDataToCloud(data: data!, localUrl: url)
                            //SwiftFileManager.deleteFileFromUrl(url: url)
                        }
                    })
                    self.backgroundQueue.addOperation(blockOperation)
                    usleep(useconds_t(QuaterSecond))
                }
            })
        }
    }

do you think usleep(useconds_t(QuaterSecond)) is good idea or is there any another better way of doing

1 Answers1

0

Try this:

DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(2), execute: {
    print("do something here")
})
Zaid M. Said
  • 314
  • 1
  • 15